Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at

  • Chris 8 posts 110 karma points c-trib
    Nov 10, 2023 @ 16:46

    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?

    Notifications interface

    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!

  • Marcio Goularte 388 posts 1360 karma points
    Nov 10, 2023 @ 22:42
    Marcio Goularte

    Hello, Yes it is possible. In last version 8 it would be through the NotificationService through the SetNotifications method

      SetNotifications(IUser user, IEntity entity, string[] actions)

    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

    enter image description here

    This is an image I took here

    where it presents Umbraco's default actions. This is because you can create your own actions

  • Chris 8 posts 110 karma points c-trib
    Nov 15, 2023 @ 00:03

    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 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)
        private void SubscribeUser(IContent content)
            // Only continue if the type of content has the alias 'contentPage'
            if (!content.ContentType.Alias.InvariantEquals("contentPage"))
            // 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;
Please Sign in or register to post replies

Write your reply to: