Playing with the Sitecore Item Saved Event

There are cases in which the data for an item should get automatically populated from a service or some kind of external database. In some cases the content editors want to decide on which items exactly should be pulled from the external service. One of the ways to achieve this is by attaching to the Sitecore Item Saved Event.

Attaching to the Item Saved Event can be achieved with two simple steps.

Step 1 – Creating a custom event handler


namespace Sandbox.Events
{
    using System;
    using Sitecore.Data;
    using Sitecore.Data.Items;
    using Sitecore.Events;
    using Sitecore.SecurityModel;

    public class ItemSavedHandler
    {
        public void OnItemSaved(object sender, EventArgs args)
        {
            // Extract the item from the event Arguments
            Item savedItem = Event.ExtractParameter(args, 0) as Item;

            // Allow only non null items and allow only items from the master database
            if (savedItem != null && savedItem.Database.Name.ToLower() == "master")
            {
                // Do some kind of template validation to limit only the items you actually want

                if (savedItem.TemplateID == ID.Parse("{00000000-0000-0000-0000-000000000000}"))
                {
                    // Get the data that you need to populate here

                    // Start Editing the Item

                    using (new SecurityDisabler())
                    {
                        savedItem.Editing.BeginEdit();

                        // Do your edits here

                        savedItem.Editing.EndEdit();
                    }
                }
            }
        }
    }
}

When inside the handler all the field values that were populated by the content editor can be accessed (for example the ID of the item in the external system).

Step 2 – Creating a custom configuration

The sample configuration can be found bellow


<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <events>
      <event name="item:saved">
        <handler type="Sandbox.Events.ItemSavedHandler, Sandbox" method="OnItemSaved">
        </handler>
      </event>
    </events>
  </sitecore>
</configuration>

The configuration is simple – it is attaching the custom code to the item:saved event.

And that`s it ! Now you are ready to attach to the item saved event and do your magic !

There are many other events in Sitecore which are a very powerful tools and allow the developers to intercept any stage of the item`s lifecyle, but they will be covered in different posts.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s