9

I am using SDL Web 8.5 and I want to broadcast a warning message to the user upon saving a component. I am using the event system to trigger the message on save. However, after setting everything up, no message appears in the message center in the user's browser when a component is saved.

EventSystem:

    EventSystem.Subscribe<Component, SaveEventArgs>(SendMessageUponSave, EventPhases.Initiated);
     private void SendMessageUponSave(Component component, ItemField itemField)
    {
        var session = component.Session;
                var message = new NotificationMessage
                {
                    Action = "example:save",
                    SubjectIds = new[] { component.Id.ToString() },
                    Details = string.Format(WARNING_STRING, x, fieldName)
            };
       session.NotificationsManager.BroadcastNotification(message);
    }
}

The NotificationHandler.js is as follows:

    console.log("The NotificationHandler.js file has been loaded by this browser.");
var notificationHandler = function(event)
{
    console.log("The NotificationHandler.js has detected that a notification event has occurred."+event.data.action );
    console.log(event.data );
    // only proceed if the message is the message we broadcasted ourself from the CM eventhandler
    if (event.data.action !== "example:save")
    {
        return;
    }
    console.log("The NotificationHandler.js has detected that a notification event has occurred with the action of example:save.");
    var userSettings = Tridion.ContentManager.UserSettings.getInstance();

    // only process the message when it is meant for this user
    if (event.data.details.creatorId === userSettings.getUserId())
    {
        if (Tridion.MessageCenter.getInstance())
        {
            var title = "Item saved.";
            var description = `Item '${event.data.details.description}' was saved`;

            Tridion.MessageCenter.registerNotification(title, description, true);
        }
    }
};
var notificationBroadcaster = Tridion.Web.UI.Core.NotificationBroadcaster.getInstance();
notificationBroadcaster.addEventListener("notification", notificationHandler);

we followed these steps to configure the UI model extension

  1. Create a new folder in the folder %Tridion Home%\Web\web\WebUI\Models\ on your SDL Web instance

  2. Update the models section in the %Tridion Home%\ Web\web\WebUI\WebRoot\Configuration\System.config configuration file to load the UI model extension

  3. Increase the modification attribute on the server element by 1 to force the browsers to get a new version of the code.

The console.log at the beginning of the NotificationHandler.js logs just fine, and the second one also logs once in awhile with the event.data.action output of 'tcm:updated' but we do not get any logs with 'example:save'. We tried checking for the answer in this TREX post: (Cannot get broadcasting to messagecenter to work that was only applicable to sites 9) but no luck.

Any clues?

4
  • Try keeping the name of your 'Action' simple to something like 'exampleSave'. It may not like the colon. Nov 19, 2019 at 21:49
  • FYI: I think the reason you see "tcm:updated" is because your handler is picking up another notification, not yours. Nov 19, 2019 at 21:58
  • How sure are you that your event handler is being run and getting to call BroadcastNotification? Have you tried debugging it or simply throwing an exception in it to make sure? Nov 26, 2019 at 15:06
  • I debugged this in visual studio, I am able my event handler is being and by inspecting the broadcast notification I can the see the message. however I'm still unable to see the notification in the CM. An thoughts? Nov 27, 2019 at 21:30

1 Answer 1

2

The reason why I was unable to get the notification in the CM was because I had not defined some of the variables in the details I was using in the notificationHandler.js

I made the following changes to my event system code to make it work.

             JObject details = JObject.FromObject(new
                {
                    creatorId = component.Creator.Id.ToString(),
                    description = "Item saved with warnings",
                    title = component.Title
                });
                var session = component.Session;
                NotificationMessage message = new NotificationMessage
                {
                    Action = "example:save",
                    SubjectIds = new[] { component.Id.ToString() },
                    Details = details.ToString()

            };
                session.NotificationsManager.BroadcastNotification(message);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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