Copied to clipboard

Flag this post as spam?

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


  • David Conlisk 432 posts 1008 karma points
    Jun 14, 2010 @ 13:05
    David Conlisk
    0

    Can I access the notification settings for a node through the api?

    Hi all,

    I'm working on some custom notification functionality. What I'd like to be able to do is to work out from a given node which users have 'signed up' for notification for changes to that node.

    Put another way, I want to get the list of users who would be notified of a particular event (e.g. publish) of a given node, using the API (i.e. in a .NET user control).

    Is this possible?
    Cheers!

    David

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Jun 14, 2010 @ 13:33
    Aaron Powell
    0

    Notifications-via-API are actually quite hard to work with, I've tried it. Compared to other aspects of Umbraco it's actually quite closed.

    The class umbraco.cms.businesslogic.workflow.Notification is the only way I've found to interact with it, but the GetNotifications method actually sends the notification.

    Your other option is umbraco.BusinessLogic.User which has a GetNotifications method does return a string which probably has some information of use...

  • David Conlisk 432 posts 1008 karma points
    Jun 14, 2010 @ 13:38
    David Conlisk
    0

    Thanks for the info @slace, I'll have a dig around in the source and see what I can find.

  • David Conlisk 432 posts 1008 karma points
    Jun 15, 2010 @ 15:45
    David Conlisk
    0

    Turns out in v4.1 you can use the GetNodeNotifications method. Here's a code snippet to get the notifications for a given node. Note that you need to check the letter to work out which action has been subscribed to (unless you want all users who have subscribed for notifications for any action on the node). See http://our.umbraco.org/projects/standard-values-in-umbraco/bug-reports/8204-Notifications-issue for a list of letters used by Umbraco. Here's the code to get the users. Here is the code:

     

             List<Notification> notifications = (List<Notification>)Notification.GetNodeNotifications(new CMSNode(nodeId));
                List<User> usersToNotify = new List<User>();

                foreach (Notification notification in notifications)
                {
                    // Check that the notification is for publish or send to publish
                    if (notification.ActionId == 'U' || notification.ActionId == 'H' || notification.ActionId == 'B')
                    {
                        User u = new User(notification.UserId);
                        usersToNotify.Add(u);
                    }
                }
    // now do something with your list of users

     

  • Shannon Deminick 1526 posts 5272 karma points MVP 3x
    Jun 15, 2010 @ 16:19
    Shannon Deminick
    0

    In 4.1 I've updated the notifications API to be 'consistent' with the rest.

    For in depth examples of all of the data APIs for 4.1, you can have a look at the source in the project Umbraco.Test

  • David Conlisk 432 posts 1008 karma points
    Oct 26, 2010 @ 15:11
    David Conlisk
    0

    Hi all,

    For anyone who's trying to achieve something similar, it turns out that the GetNotifications() call is not recursive, so you need to recurse the tree yourself in order to get any notifications that cascade down from parent nodes.

    Here's the code I've used to create a comma-separated list of email addresses to send notifications to:

    CMSNode n = new CMSNode(nodeId);
                List<Notification> notifications = new List<Notification>();
                GetNotifications(n, ref notifications);
                foreach (Notification notification in notifications)
                {
                    // Check that the notification is for publish or send to publish
                    if (notification.ActionId == 'U' || notification.ActionId == 'H' || notification.ActionId == 'B')
                    {
                        User u = new User(notification.UserId);
                        if (u.Email != "" && recipients.IndexOf(u.Email + ",") == -1)
                        {
                            recipients += u.Email + ",";
                        }
                    }
                }
                recipients = recipients.TrimEnd(',');

    And this is the recursive function that creates the list of notifications for all nodes from the current one up the tree:

    private void GetNotifications(CMSNode n, ref List<Notification> notifications) {
                //CMSNode n = new CMSNode(nodeId);
                notifications.AddRange(Notification.GetNodeNotifications(n));
                if (n.ParentId != -1)
                {
                    GetNotifications(n.Parent, ref notifications);
                }
            }

     

Please Sign in or register to post replies

Write your reply to:

Draft