Sitecore NotificationProvider.GetNotifications is empty

本文关键字:empty is GetNotifications NotificationProvider Sitecore | 更新日期: 2023-09-27 18:20:31

更改源项后,我正在尝试获取克隆上的通知。在我们的系统中,当源项目更改时,克隆也会自动更改。但是,我们需要自动拒绝Sitecore通知,即"原始项目中的字段已更改",并提供审查/接受/拒绝选项。问题是,在克隆上使用GetNotifications()会返回0个元素,这意味着Sitecore没有找到任何通知。但是,当我重新加载/重新打开克隆时,我可以清楚地看到它们。

我试着用重新加载物品

item.Reload();

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

在运行GetNotifications()之前,但都没有使通知数大于零。

这是我使用的完整代码(其中copyItem是我的克隆)int k是一个测试,它返回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);
                }
            }
        }

注意:我在OnItemSaved事件下面调用上面的代码。

Sitecore NotificationProvider.GetNotifications is empty

我发现了一篇有用的帖子,它提供了与我使用的代码略有不同的内容。它使用Sitecore作业(异步/后台进程)来运行通知检查,并且也有轻微的延迟。这对我来说现在似乎很有效。最后,这些通知不再出现了!

我最后的代码是:

    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();
                }
            }
        }
    }

注意:ReReference代码与此解决方案无关,您不需要它。