Sitecore: mystery event that never fires (template:updated)

I needed to watch changes happening to a template so I could know about any newly added fields. These changes are triggered from an external PIM system which changes the Product or Resource templates when a new field is added to a Product for example. I’ll spare you the details of why I needed to know when this happened but I thought Sitecore has to have a way to handle this. I had used the item:saved event to handle item updates in the past but I had never had to watch for changes to a template. I opened up Sitecore.config in a text editor and started looking at the list of events in the section. Somewhere in the middle of this section, after the usual item related events, I noticed the “template:updated” event with no handler specified. I was sure that all I needed to do now was to implement an event handler in a class and hook up the handler here (of course using a patch file). But this event never gets fired for some reason. Doubting my patch files expertise I even tried directly editing the Sitecore.config but no luck. Then I though may be the template:update event is supposed to fire when an item’s template is changed but we have the item:templateChanged event which works as expected.

I could find no documentation on this event anywhere. Looking at the Event class in the Sitecore.Events namespace of the Sitecore.Kernel.dll (8.1) assembly under DotPeek I could not find any reference to the “template:updated” event, but I did see plenty of the item:* events (item:saved, item:renamed, item:deleted, etc.) in there. It has been there in the config file since Sitecore 6.6 at least. So why is this event in the Sitecore.config or web.config for 6.6? Let me know in the comments if anybody knows how to make this work or why its in the config file but not supported in the code at all.

Here is what I was using to test it and at the bottom I have a solution to work around this problem.

Simple Event Handler:

1
2
3
4
5
6
7
8
public class TemplateItemUpdateHandler
{
      public void OnTemplateUpdate(object sender, EventArgs args)
      {
          var item = Event.ExtractParameter(args, 0) as Item;
          Sitecore.Diagnotics.Log.Info("Inside the Item Update event", this);
      }
}

Patch File:

1
2
3
4
5
6
7
8
9
10
11
12
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <events>
      <event  name="template:updated">
        <patch:delete />
      </event>
      <event name="template:updated">
        <handler type="ClientName.Sitecore.Custom.Events.EventHandlers.TemplateItemUpdateHandler, ClientName.Sitecore.Custom.Events" method="OnTemplateUpdate" />
      </event>
    </events>
  </sitecore>
</configuration>

ShowConfig.aspx Output:

Test Template Change

Solution:
I ended up using the item:saved event to handle this but had to do some less than ideal Guid checking in the code. When a new field is added to the Product template the item:saved event is fired and I basically check the existence of Product Template in up the tree. The drawback of this that my handler code now runs every time any item is saved with a possible performance hit.

1
2
3
4
5
6
7
8
9
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <events>
      <event name="item:saved">
        <handler type="ClientName.Sitecore.Custom.Events.EventHandlers.TemplateItemUpdateHandler, ClientName.Sitecore.Custom.Events" method="OnTemplateUpdate"/>
      </event>
    </events>
  </sitecore>
</configuration>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
        public void OnTemplateUpdate(object sender, EventArgs args)
        {
            var item = Event.ExtractParameter(args, 0) as Item;

            if (item != null
                && item.Database == SitecoreUtility.GetMasterDatabase()
                && item.TemplateID.Guid == TemplateIds.TemplateFieldTemplateId
                && item.Parent != null && item.Parent.Parent != null
                && item.Parent.Parent.ID.Guid == TemplateIds.ProductTemplateId)
            {
                using (new SecurityDisabler())
                {
                    createNewPIMFieldMappings(item);
                }
            }
        }

Pages that mention this event:
Alan Coates – Sitecore/.NET blog: Sitecore Save Event