Reputation: 1013
I'm working with Sitecore 8 Update 2. I wanted to collect information about what is happening to the Sitecore items ( created, saved, deleted, published,... ).
In order to achieve this I went and set up multiple eventhandlers with their respective event. All sitecore events are located in the config under configuration --> sitecore -> events.
Here i was able to add handlers ( see SaveItemActions.config ) to events such as "item:added", "item:saved" and "publish:complete". These eventhandlers now point to their respective methods in my new class ( SaveItemActions.cs ).
Unfortunately i was not able to add a handler to "item:created", "item:deleted", "item:versionAdded" and "item:versionRemoved" because i end up with the following error after adding these.
Could not instantiate event handler. Type: Sitecore.Links.ItemEventHandler. Method: OnItemDeleted> (method: Sitecore.Events.Event.GetConfigSubscribers()).
But it seems like i should be able to add handlers to these events aswell as they already have handlers provided by Sitecore. For example from /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>
I decompiled Sitecore.Kernel.dll to look at Sitecore.Links.ItemEventHandler ( see below ) and couldn't find anything special about it.
If anyone knows what causes this or has any kind of feedback, feel free to let me know.
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);
}
}
}
}
Upvotes: 1
Views: 1652
Reputation: 27132
All your code is ok. It's exactly how it's supposed to be written.
The only problem are typos in your configuration. You have extra >
characters in method
attributes for 3 of your handlers (e.g. 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>" />
Upvotes: 2