3

I am trying to add a custom notification message to display in the preview mode in Sitecore 8 for a particular item. I have done some digging and found you can create custom notification message extending the etPageEditorNotificationsProcessor class.

As descriped in this: https://techmusingz.wordpress.com/2016/11/09/add-workflow-notifications-for-all-editable-items-on-a-sitecore-page/

I have managed to do this but the custom message only shows in the Experience Editor not the Preview mode. Does anyone know how I can accomplish this in the Preview mode please? Something similar to below:

enter image description here

3

Unfortunately RenderNotifications method of WebEditRibbonForm class contains check:

if (WebUtil.GetQueryString("mode") != "edit")
    return;

The simplest solution would be to override WebEditRibbonForm, replace CodeBeside from the original WebEditorRibbonForm to your custom class in sitecore\shell\Applications\WebEdit\WebEditRibbon.aspx file and write few methods like below to make sure that Sitecore renders notifications in preview mode as well:

public class ExtendedWebEditorRibbonForm : WebEditRibbonForm
{
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        ItemUri queryString = ItemUri.ParseQueryString();
        if (!Context.ClientPage.IsEvent)
        {
            Item item = Database.GetItem(queryString);
            if (item != null)
            {
                RenderNotifications(item);
            }
        }
    }
    private void RenderNotifications(Item item)
    {
        Assert.ArgumentNotNull(item, nameof(item));
        // in preview mode we do same what Sitecore does in edit mode 
        if (WebUtil.GetQueryString("mode") != "preview")
            return;

        List<PageEditorNotification> editorNotifications = ItemUtility.GetPageEditorNotifications(item);
        if (editorNotifications.Count == 0)
        {
            Notifications.Visible = false;
        }
        else
        {
            HtmlTextWriter output = new HtmlTextWriter(new StringWriter());
            int count = editorNotifications.Count;
            for (int index = 0; index < count; ++index)
            {
                PageEditorNotification notification = editorNotifications[index];
                string empty = string.Empty;
                if (index == 0)
                    empty += " First";
                if (index == count - 1)
                    empty += " Last";
                RenderNotification(output, notification, empty);
            }
            Notifications.InnerHtml = output.InnerWriter.ToString();
        }
    }

    private static void RenderNotification(HtmlTextWriter output, PageEditorNotification notification, string marker)
    {
        Assert.ArgumentNotNull(output, nameof(output));
        Assert.ArgumentNotNull(notification, nameof(notification));
        Assert.ArgumentNotNull(marker, nameof(marker));
        string str = Themes.MapTheme(notification.Icon ?? GetNotificationIcon(notification.Type));
        output.Write("<div class=\"scPageEditorNotification {0}{1}\">", notification.Type, marker);
        output.Write("<img class=\"Icon\" src=\"{0}\"/>", str);
        output.Write("<div class=\"Description\">{0}</div>", notification.Description);
        foreach (PageEditorNotificationOption option in notification.Options)
            output.Write("<a onclick=\"javascript: return scForm.postEvent(this, event, '{0}')\" href=\"#\" class=\"OptionTitle\">{1}</a>", option.Command, option.Title);
        output.Write("<br style=\"clear: both\"/>");
        output.Write("</div>");
    }

    private static string GetNotificationIcon(PageEditorNotificationType notificationType)
    {
        switch (notificationType)
        {
            case PageEditorNotificationType.Error:
                return "Custom/16x16/error.png";
            case PageEditorNotificationType.Information:
                return "Custom/16x16/info.png";
            default:
                return "Custom/16x16/warning.png";
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.