无法为某些Sitecore事件添加处理程序

时间:2016-12-22 10:08:21

标签: c# events sitecore config eventhandler

我正在使用Sitecore 8 Update 2。 我想收集有关Sitecore项目(创建,保存,删除,发布,......)的信息。

为了达到这个目的,我去了各自的事件,设置了多个事件处理程序。所有sitecore事件都位于配置下的配置中 - > sitecore - >事件

在这里,我能够将处理程序(请参阅SaveItemActions.config)添加到诸如" item:added"," item:saved"之类的事件中。和"发布:完成"。这些事件处理程序现在指向我的新类(SaveItemActions.cs)中的各自方法。

不幸的是,我无法为" item:created"," item:deleted"," item:versionAdded"添加处理程序。和" item:versionRemoved"因为我在添加这些后最终会出现以下错误。

Could not instantiate event handler. Type: Sitecore.Links.ItemEventHandler. Method: OnItemDeleted> (method: Sitecore.Events.Event.GetConfigSubscribers()).

但似乎我应该能够为这些事件添加处理程序,以及它们已经拥有Sitecore提供的处理程序。例如来自/sitecore/admin/showconfix.aspx:

<event name="item:deleted">
    <handler type="Sitecore.Links.ItemEventHandler, Sitecore.Kernel" method="OnItemDeleted"/>
    <handler type="Sitecore.Tasks.ItemEventHandler, Sitecore.Kernel" method="OnItemDeleted"/>
    <handler type="Sitecore.Globalization.ItemEventHandler, Sitecore.Kernel" method="OnItemDeleted"/>
    <handler type="Sitecore.Data.Fields.ItemEventHandler, Sitecore.Kernel" method="OnItemDeleted"/>
    <handler type="Sitecore.Rules.ItemEventHandler, Sitecore.Kernel" method="OnItemDeleted"/>
    <handler type="Sitecore.Caching.Placeholders.PlaceholderCacheManager, Sitecore.Kernel" method="UpdateCaches"/>
    <handler type="be.absi.kbs.web.Helpers.Processors.SaveItemActions, be.absi.kbs.web" method="OnItemDeleted>" patch:source="SaveItemActions.config"/>
    <handler type="Sitecore.Modules.EmailCampaign.Core.RootItemEventHandler, Sitecore.EmailCampaign" method="OnRootDeleted" patch:source="Sitecore.EmailExperience.ContentManagement.config"/>
</event>

我反编译Sitecore.Kernel.dll以查看Sitecore.Links.ItemEventHandler(见下文)并且找不到任何特别的内容。

如果有人知道是什么导致这种情况或有任何反馈意见,请随时告诉我。

SaveItemActions.config

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
   <events>

  <event name="item:added">
    <handler type="be.absi.kbs.web.Helpers.Processors.SaveItemActions, be.absi.kbs.web" method="OnItemAdded" />
  </event>

  <!-- event name="item:created">
    <handler type="be.absi.kbs.web.Helpers.Processors.SaveItemActions, be.absi.kbs.web" method="OnItemCreated>" />
  </event-->

  <event name="item:saved">
    <handler type="be.absi.kbs.web.Helpers.Processors.SaveItemActions, be.absi.kbs.web" method="OnItemSaved" />
  </event>

  <event name="publish:complete">
    <handler type="be.absi.kbs.web.Helpers.Processors.SaveItemActions, be.absi.kbs.web" method="OnItemPublished" />
  </event>

  <!-- event name="item:deleted">
    <handler type="Sitecore.Links.ItemEventHandler, Sitecore.Kernel" method="OnItemDeleted>" />
  </event-->

  <!-- event name="item:versionAdded">
    <handler type="be.absi.kbs.web.Helpers.Processors.SaveItemActions, be.absi.kbs.web" method="OnVersionAdded>" />
  </event-->

  <!-- event name="item:versionRemoved">
    <handler type="be.absi.kbs.web.Helpers.Processors.SaveItemActions, be.absi.kbs.web" method="OnVersionRemoved>" />
  </event-->

  </events>
 </sitecore>
</configuration>

SaveItemActions.cs

using Sitecore.Data.Items;
using Sitecore.Events;
using Sitecore.Pipelines.Save;
using Sitecore.Publishing.Pipelines.PublishItem;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace be.absi.kbs.web.Helpers.Processors
{
    public class SaveItemActions 
    {
        public void OnItemSaved(object sender, EventArgs args)
        {
            // Extract the item from the event Arguments
            Item savedItem = Event.ExtractParameter(args, 0) as Item;
        }

        public void OnItemAdded(object sender, EventArgs args)
        {

        }

        public void OnItemCreated(object sender, EventArgs args)
        {

        }

        public void OnItemPublished(object sender, EventArgs args)
        {

        }

        public void OnItemDeleted(object sender, EventArgs args)
        {

        }

        public void OnVersionAdded(object sender, EventArgs args)
        {

        }

        public void OnVersionRemoved(object sender, EventArgs args)
        {

        }
    }
}

ItemEventHandler.cs

namespace Sitecore.Links
{
    public class ItemEventHandler
    {
        /*
        Bunch of other methods....
        */

        protected void OnItemDeleted(object sender, EventArgs args)
        {
            if (args == null)
            {
                return;
            }
            if (!Settings.LinkDatabase.UpdateDuringPublish && PublishHelper.IsPublishing())
            {
                return;
            }
            Item item = Event.ExtractParameter(args, 0) as Item;
            Assert.IsNotNull(item, "No item in parameters");
            LinkDatabase linkDatabase = ItemEventHandler.LinkDatabase;
            if (linkDatabase != null)
            {
                linkDatabase.RemoveReferences(item);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您的所有代码都可以。它应该是如何编写的。

唯一的问题是配置中的拼写错误。 3个处理程序的>属性中有额外method个字符(例如method="OnItemDeleted>"):

<handler type="Sitecore.Links.ItemEventHandler, Sitecore.Kernel" method="OnItemDeleted>" />
<handler type="be.absi.kbs.web.Helpers.Processors.SaveItemActions, be.absi.kbs.web" method="OnVersionAdded>" />
<handler type="be.absi.kbs.web.Helpers.Processors.SaveItemActions, be.absi.kbs.web" method="OnVersionRemoved>" />