每当添加一个项目时,Sitecore 都会添加几个孤立项目

标签 sitecore sitecore6 orphan

在我的 Sitecore 6.1.0 安装中,我通过实现我自己的自定义处理程序(在 Web.config 中)连接到“item: added”事件:

   <event name="item:added">
    <handler type="Sitecore.Data.Fields.ItemEventHandler, Sitecore.Kernel" method="OnItemAdded" />
    <handler type="my.project.Classes.OnSaveItemHandler, my.project" method="OnItemAdded" />
   </event>

这样做的目的是强制项目的唯一名称 - 换句话说,在我的 OnItemAdded 方法中,我想对与添加的项目同名的任何其他项目进行 Lucene 搜索。

每次在 Sitecore 结构中添加项目时都会调用 OnItemAdded 方法。但我的问题是 - 该方法被多次调用 每件 .我已经看到它在每个添加的项目中调用 6 到 26 次,具体取决于我在 Sitecore 结构中添加项目的位置。我的 OnItemAdded 方法的主体是空的:
    protected void OnItemAdded(object obj, EventArgs args)
    {
    }

添加项目时第一次调用该方法时,args 中的项目参数是正确的项目。如果项目的名称是 theItemName ,FullPath 属性将如下所示:
/sitecore/content/theItemName

每次除了第一个,项目 外观正确,但项目的路径如下所示:
[orphan]/sitecore/content/theItemName

为什么将 [orphan] 位添加到完整路径中?为什么 OnItemAdded 方法被调用不止一次,即使我只添加了一项?

最佳答案

我相信像这样的事件存在已知问题,其中该方法将被多次调用。我知道我有过类似的经历,我试图在创建项目时以编程方式为项目创建角色。 John West said the following as a precaution :

I seem to remember that Sitecore sometimes fires some events more than once, so you might want to check for that [...]



然后,John 提供了指向名为 Intercepting Item Updates with Sitecore 的博客文章的链接。

在我的代码中,我进行了检查以查看我正在寻找的操作是否已经发生,例如该项目的角色是否存在。在您的情况下,检查方法可能有点困难。也许你可以做一些偷偷摸摸的事情,比如:
protected void OnItemAdded(object obj, EventArgs args) {
  Item item = // code to extract item from args, I forgot it

  if(item.Paths.FullPath.StartsWith("/sitecore/content")) {
    // do your stuff because you know its the first time the event fired
  }
}

再次,这是非常hackish的。如果 Sitecore 支持无法提供任何更好的选项(或者堆栈溢出上没有发布任何更好的选项),我会说这是最后的手段。

关于每当添加一个项目时,Sitecore 都会添加几个孤立项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6138754/

相关文章:

Azure App Service 随机给出 SSL 连接错误

Sitecore Axes.IsDescendantOf/Axes.IsAncestorOf - 包括在内?

c# - Visual Studio Sitecore TDS 无法打开 .scproj 项目

sql-server-2008 - Sitecore 数据库清理错误 - 超时?

c# - Sitecore.Context.Item 是否使用 HttpContext?

java - JPA 2.0/Hibernate 和 "orphanRemoval": Just replacing an entity does not remove the old one

c# - NHibernate:更改父级 - "deleted object would be re-saved by cascade"

java - 级联 ="all-delete-orphan"在与连接表的 Hibernate 单向多对多关联中有任何意义吗?

networking - http 重定向到 https

c# - 替换 Sitecore 媒体库链接中的空格