Programmatically adding users to backoffice email notifications
Is it possible to programmatically sign users up for email notifications? I know that it is possible in the backoffice (screenshot below) to select which email notifications you receive when content is modified, but is it possible to do this in code?
My use case is that I have a small number of admin backoffice users, and another group of writer users who can create and modify a specific content type. If the writers create or modify any of these pieces of content, the admin users would like to be notified by email.
I'd like to have the admin users automatically signed up to email notifications for this content type if they are created/modified, so that they don't have to manually opt-in to notifications for each of these pieces of content.
Is this possible, and are there any resources anyone could share that would point me in the right direction? Thanks!
And each type of action you pass through the string array. Each action is represented by a character. And you can see each of these default permissions here
I found that the SetNotifications method can override other notifications set by a user for a specific piece of content, so if the user had previously turned on a different notification and we use SetNotifications in the wrong way, we can end up turning off the other notifications for that user.
I found that you can use CreateNotification instead to turn on a specific notification without affecting the others.
Ended up with something like the code below, hopefully this can help someone if they ever come across it :)
My example should turn on the Publish notification whenever a piece of content with the alias 'contentPage' is saved, so if a new contentPage is created our user will automatically have the Publish notification turned on
public class NotificationSubscribeComposer : ComponentComposer<NotificationSubscriberComponent> { }
public class NotificationSubscriberComponent : IComponent
{
private readonly INotificationService _notificationService;
private readonly IUserService _userService;
public NotificationSubscriberComponent(INotificationService notificationService, IUserService userService)
{
_notificationService = notificationService;
_userService = userService;
}
// Add event handler for when any content is saved (or saved and published)
public void Initialize() => ContentService.Saved += ContentService_Saved;
private void ContentService_Saved(IContentService sender, ContentSavedEventArgs e)
{
// We might be saving multiple pieces of content at once, so loop through them :)
foreach (var content in e.SavedEntities)
{
SubscribeUser(content);
}
}
private void SubscribeUser(IContent content)
{
// Only continue if the type of content has the alias 'contentPage'
if (!content.ContentType.Alias.InvariantEquals("contentPage"))
{
return;
}
// Get the user or users you want here
var user = _userService.GetUserById(-1);
// Get the user's current notifications
var currentNotifications = _notificationService.GetUserNotifications(user);
// Check if a publish notification for this content already exists
if (currentNotifications.Any(n => n.EntityId == content.Id && n.Action.Equals(ActionPublish.ActionLetter.ToString())))
{
return; // Don't create the notification for the user if one already exists
}
// Create the notification so the user receives an email any time the content is published
_notificationService.CreateNotification(user, content, ActionPublish.ActionLetter.ToString());
}
public void Terminate() => ContentService.Saved -= ContentService_Saved;
}
Programmatically adding users to backoffice email notifications
Is it possible to programmatically sign users up for email notifications? I know that it is possible in the backoffice (screenshot below) to select which email notifications you receive when content is modified, but is it possible to do this in code?
My use case is that I have a small number of admin backoffice users, and another group of writer users who can create and modify a specific content type. If the writers create or modify any of these pieces of content, the admin users would like to be notified by email.
I'd like to have the admin users automatically signed up to email notifications for this content type if they are created/modified, so that they don't have to manually opt-in to notifications for each of these pieces of content.
Is this possible, and are there any resources anyone could share that would point me in the right direction? Thanks!
Hello, Yes it is possible. In last version 8 it would be through the NotificationService through the SetNotifications method
https://github.com/umbraco/Umbraco-CMS/blob/53395db518d59e2b77971922ff31f2b2f0b5b50b/src/Umbraco.Core/Services/Implement/NotificationService.cs#L244
And each type of action you pass through the string array. Each action is represented by a character. And you can see each of these default permissions here
https://github.com/umbraco/Umbraco-CMS/tree/e5c8627abf7984f6939d17105f581304dcdf4553/src/Umbraco.Core/Actions
This is an image I took here https://our.umbraco.com/forum/using-umbraco-and-getting-started/104930-creating-my-custom-iactions
where it presents Umbraco's default actions. This is because you can create your own actions
Thanks Marcio!
I found that the
SetNotifications
method can override other notifications set by a user for a specific piece of content, so if the user had previously turned on a different notification and we useSetNotifications
in the wrong way, we can end up turning off the other notifications for that user.I found that you can use
CreateNotification
instead to turn on a specific notification without affecting the others.Ended up with something like the code below, hopefully this can help someone if they ever come across it :)
My example should turn on the Publish notification whenever a piece of content with the alias 'contentPage' is saved, so if a new contentPage is created our user will automatically have the Publish notification turned on
is working on a reply...