c# - 无论如何,sitecore 中是否自动接受克隆通知?

标签 c# sitecore

我有一个 sitecore 项目,它是从另一个 sitecore 项目克隆而来的。当我将子项目添加到父项时,我希望它也将项目添加到克隆下。它这样做但我必须手动接受每个更改。我有 250 个克隆!

enter image description here 有没有一种方法可以自动接受这些通知或首先阻止它们这样做?

最佳答案

this article from sitecore评论区有人问了类似的问题:

When you create a new subitem for an item that has been clones, it seems you have to manually go to each clone and accept that a subitem be created beneath the clone. Is there any way to not have to do this? For a new solution we will have several clones of one source and the customer won't want to go to every single clone and accept a subitem. It would be much easier and user friendly to have subitems always appear in the clones without any questions. The same also applies to instances where content authors has overridden a value in a clone, then a "power author" changes a value in the source item. How can this change be applied to all clones without the clones having to manually accept the change? Cheers

sitecore 发布了带有一些伪代码的回复。基本上这转化为以下内容:

构建一个事件类

public abstract class AcceptCloneNotificationsEventBase<T> where T : Notification
{
    protected abstract bool ShouldAcceptNotification(Item item, Item parent);

    public void AcceptClone_SavedItem(object sender, EventArgs args)
    {
        var item = (Item)Event.ExtractParameter(args, 0);
        var parent = item.Parent;
        foreach (var clone in item.GetClones())
        {
            foreach (var notification in clone.Database.NotificationProvider.GetNotifications(clone.Uri))
            {
                if (notification is T && ShouldAcceptNotification(item, parent)) 
                {
                    notification.Accept(clone);
                }
            }
        }
    }

    public void AcceptClone_CreateItem(object sender, EventArgs args)
    {
        var item = (Item)Event.ExtractParameter(args, 0);
        var parent = item.Parent;
        foreach (var clone in parent.GetClones())
        {
            foreach (var notification in clone.Database.NotificationProvider.GetNotifications(clone.Uri))
            {
                if (notification is T && ShouldAcceptNotification(item, parent)) 
                {
                    notification.Accept(clone);
                }
            }
        }
    }
}

我将其构建为一个抽象类,因此我可以多次使用它。实现看起来像:

public class ImplementationOfBase: 
             AcceptCloneNotificationsEventBase<FieldChangedNotification>
{
    protected override bool ShouldAcceptNotification(Item item, Item parent)
    {
        return /*filter the event as you see fit here*/;
    }
}

注册事件

<events>
  <event name="item:added">
    <handler type="Namespace.ImplementationOfBase,Namespace" method="AcceptClone_CreateItem"/>
  </event>
  <event name="item:saved">
    <handler type="Namespace.ImplementationOfBase,Namespace" method="AcceptClone_SavedItem"/>
  </event>
</events>

关于c# - 无论如何,sitecore 中是否自动接受克隆通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34359090/

相关文章:

sitecore:绑定(bind)项目链接

asp.net - 全新安装中的配置文件无效 - 缺少配置部分 'contactRepository'

c# - 不同语言的总结 C#

c# - 如何避免异常反序列化无效的枚举项?

c# - 复制一个更大的位数组(右对齐)

.net - 是否可以在 Linux 或 Mac 上的 Mono 上安装 Sitecore CMS?

c# - Directory.GetCurrentDirectory() 不返回正确的目录

c# - 获取不带url参数的MVC路由路径

c# - 玻璃映射器渲染图像数据属性

xslt - 尝试在 Sitecore 中进行 XSLT 排序后获取第一项