Exam Area: Content Area 4 – Framework Components (15%)
Reference: https://docs.developers.optimizely.com/content-management-system/docs/using-notifications
The CMS has a User Notifications system for sending notifications to users directly within the CMS UI (bell icon).
using EPiServer.Notification;
public class NotificationService
{
private readonly INotificationRepository _notificationRepository;
public NotificationService(INotificationRepository notificationRepository)
{
_notificationRepository = notificationRepository;
}
// Send a notification
public async Task SendNotificationAsync(string recipientUsername, string message)
{
var notification = new NotificationMessage
{
ChannelName = "epi.cms.contentediting.notifications",
TypeName = "contentapproval",
Sender = new NotificationUser { UserName = "system" },
Recipient = new NotificationUser { UserName = recipientUsername },
Content = message,
// Optional: link
SendEmail = false,
SendOnChannel = true
};
await _notificationRepository.CreateAsync(notification);
}
}
// Format a notification for display in the UI
[ServiceConfiguration(typeof(INotificationFormatter))]
public class MyNotificationFormatter : INotificationFormatter
{
public string ChannelName => "my.custom.channel";
public NotificationMessage FormatMessage(
NotificationMessage message, string recipient)
{
// Customize the notification message
message.Content = $"[MY APP] {message.Content}";
return message;
}
}
The CMS automatically sends notifications when:
// Configure email notifications for content approvals
// Admin → Config → Content Approvals → Email Settings
// Subscribe to specific notification types
public class NotificationSubscriber
{
private readonly INotificationRepository _repo;
public async Task SubscribeToApprovals(string username)
{
var subscription = new NotificationSubscription
{
UserName = username,
Enabled = true
};
await _repo.SubscribeAsync(subscription,
"epi.cms.contentediting.notifications");
}
}
// In a workflow: Send a notification when content needs review
[InitializableModule]
public class WorkflowNotificationModule : IInitializableModule
{
private IContentEvents _contentEvents;
private INotificationRepository _notificationRepo;
public void Initialize(InitializationEngine context)
{
_contentEvents = context.Locate.Advanced.GetInstance<IContentEvents>();
_notificationRepo = context.Locate.Advanced.GetInstance<INotificationRepository>();
_contentEvents.RequestingApproval += OnRequestingApproval;
}
private async void OnRequestingApproval(object sender, ContentEventArgs e)
{
var notification = new NotificationMessage
{
ChannelName = "epi.cms.contentediting.notifications",
TypeName = "contentapproval",
Sender = new NotificationUser { UserName = e.RequiredRole ?? "system" },
Recipient = new NotificationUser { UserName = "approver@example.com" },
Content = $"Content '{e.Content?.Name}' needs approval",
SendEmail = true,
SendOnChannel = true
};
await _notificationRepo.CreateAsync(notification);
}
public void Uninitialize(InitializationEngine context)
{
_contentEvents.RequestingApproval -= OnRequestingApproval;
}
}
// appsettings.json - Email configuration
{
"EPiServer": {
"Framework": {
"EmailSettings": {
"Host": "smtp.example.com",
"Port": 587,
"EnableSsl": true,
"UserName": "cms@example.com",
"Password": "secret",
"From": "CMS System <cms@example.com>"
}
}
}
}
INotificationRepository)SendEmail and SendOnChannel do in NotificationMessage?
SendEmail: Sends an email notificationSendOnChannel: Displays the notification in the CMS UIINotificationRepository.SubscribeAsync())