0
votes

I am trying to get the notifications on a clone after I change the source item. In our system, when the source item changes, the clone automatically changes as well. However we need to auto-reject the Sitecore notification that says, "A field in the original item has been changed" and gives the review/accept/reject options. The problem is that using GetNotifications() on the clone returns 0 elements - meaning Sitecore didn't find any notifications. However I clearly see them when I reload/re-open the clone.

I tried reloading the item using:

item.Reload();

and

Context.ClientPage.SendMessage(this, "item:load(id=" + item.ID + ")");

before running GetNotifications(), but neither made the count of notifications greater than zero.

Here is my full code used (where copyItem is my clone) int k is a test and it returns 0.

        using (new SecurityDisabler())
        {
            if (copyItem.IsClone)
            {
                var notifies = Database.GetDatabase("master").NotificationProvider.GetNotifications(copyItem);

                int k = -1;

                if (notifies != null) k = notifies.Count();

                foreach (Notification n in notifies)
                {
                    n.Reject(copyItem);
                }
            }
        }

Note: I'm calling the above code underneath the OnItemSaved event.

1

1 Answers

0
votes

I found a helpful post that provided something slightly different from the code I was using. It uses a Sitecore job (async/background process) to run the notifications check and also has a slight delay. This seems to be working well for me now. Finally those notifications aren't showing up anymore!

My final code was:

    public void OnItemSaved(object sender, EventArgs args)
    {
        var item = Event.ExtractParameter(args, 0) as Item;

        ReReferenceFieldAndRemoveNotifications(item, args);

        ...
    }

    private void ReReferenceFieldAndRemoveNotifications(Item srcItem, EventArgs args)
    {
        if (srcItem != null && !srcItem.Paths.Path.ToLower().Contains(string.Format("{0}/{1}", "content", "canada")))
        {
            var destItem = Database.GetDatabase("master").GetItem(srcItem.Paths.Path.Replace("United States", "Canada"));

            // Update the clone
            Rereferencer.RereferenceFields(srcItem, destItem);

            // Now reject the notifications on the clone (accepting would push the US values which we don't want)
            using (new SecurityDisabler())
            {
                if (srcItem.HasClones)
                {
                    var jobOptions = new JobOptions("RejectNotifications", string.Empty, Context.GetSiteName(), this, "RejectNotifications", new object[] { srcItem });
                    var job = new Job(jobOptions);
                    jobOptions.InitialDelay = new TimeSpan(0, 0, 0, 1, 0);
                    JobManager.Start(job);
                }
            }
        }
    }

    public void RejectNotifications(Item args)
    {
        // Remove and reject any notifications on the clone.
        using (new SecurityDisabler())
        {
            var item = args;
            var clones = item.GetClones(true);
            foreach (var clone in clones)
            {
                var notifications = clone.Database.NotificationProvider.GetNotifications(clone);
                foreach (var notification in notifications)
                {
                    clone.Database.NotificationProvider.RemoveNotification(notification.ID);
                    notification.Reject(clone);
                }

                clone.Editing.BeginEdit();
                try
                {
                    clone.Fields["__Workflow"].Value = args.Fields["__Workflow"].Value;
                    clone.Fields["__Workflow state"].Value = args.Fields["__Workflow state"].Value;
                }
                finally
                {
                    clone.Editing.EndEdit();
                }

            }
        }
    }

Note: The ReReference code is not related to this solution, you do not need that.