Notification broadcasting Event Handler example
To broadcast a notification about an Event occurring, to be picked up by, for example, an extension to a Tridion Sites GUI, create an Event Handler that uses the BroadcastNotification method of the NotificationManager class.
The following example broadcasts a notification whenever a Component gets deleted, announcing that the Component was deleted, and by whom. Note the use of the Tridion.ContentManager.Notifications namespace.
using System;
using System.Text;
using Tridion.ContentManager.Extensibility.Events;
using Tridion.ContentManager.Extensibility;
using Tridion.ContentManager.ContentManagement;
using Tridion.ContentManager.Notifications;
namespace MyEventHandlers
{
[TcmExtension("MyEventHandlerExtension")]
public class MyEventHandler : TcmExtension
{
public MyEventHandler()
{
Subscribe();
}
public void Subscribe()
{
EventSystem.Subscribe<Component, DeleteEventArgs>(HandlerForCommitted, EventPhases.TransactionCommitted);
}
private void HandlerForCommitted(Component subject, DeleteEventArgs args, EventPhases phase)
{
var session = subject.Session;
var message = new NotificationMessage
{
Action = "Delete",
SubjectIds = new[] { subject.Id.ToString() },
Details = string.Format("Component was deleted by {0}", session.User.Description)
};
session.NotificationsManager.BroadcastNotification(message);
}
}
}