When working with Sitecore in a multi developer environment, you need a method for sharing items across your solutions. At Oxygen Software we have decided to use Item Serialization for the purpose. We do this, because every developer has a local database for the solution installed, and every developer has been thought that the first thing you do, when you start working on a project for the day, is to update your serialization folder, and update the database in Sitecore.

One of the challenges we have found with the strategy, is that developers seems to forget to serialize their items, when they create new items. Sitecore has provided a solution for the problem, as they already has made event handlers for 4 events; saved, deleted, moved and verisonRemoved. You can enable the default serialization by including the following .config in the include folder:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore>
        <events>
            <event name="item:saved">
                <handler type="Sitecore.Data.Serialization.ItemHandler, Sitecore.Kernel" method="OnItemSaved"/>
            </event>
            <event name="item:deleted">
                <handler type="Sitecore.Data.Serialization.ItemHandler, Sitecore.Kernel" method="OnItemDeleted"/>
            </event>
            <event name="item:moved">
                <handler type="Sitecore.Data.Serialization.ItemHandler, Sitecore.Kernel" method="OnItemMoved"/>
            </event>
            <event name="item:versionRemoved">
                <handler type="Sitecore.Data.Serialization.ItemHandler, Sitecore.Kernel" method="OnItemVersionRemoved"/>
            </event>
        </events>
    </sitecore>
</configuration>

The include file, enables that every time you do anything with an item, its being serialized, it takes care of creation, deleting and moving of the items. In general, the config is fine, but it still needs a few tweaks to be usable for our purposes.

We have tweaked the save element to enable two tings

  1. You can specify which paths that should be serialized. I.e. normal content shouldn’t be automatically serialized, but templates, layouts and system setting should.
  2. Serialize the ancestor path. This is done, as you need the entire ancestor path to deserialize items. You need the parent of the item you want to deserialize, to figure where in the Sitecore tree it should be deserialized

The two features are implemented by creating the following handler for OnItemSaved event

public class SerializeEventHandler
    {

        public void OnItemSaved(object obj, EventArgs args)
        {
            Item item = Sitecore.Events.Event.ExtractParameter(args, 0) as Item;
            if (item == null || item.Template == null)
            {
                return;
            }
            // check if the item should be serialized (paths is defined in config files)
            if(SerializeMe(item))
            {
                // serialize item and parent path
                SerializeItemAndParent(item);
            }
        }

        /// <summary>
        /// Handler to save the item and its parent, will be called recursive to sage the entire ancestor tree
        /// </summary>
        /// <param name="item">the item to serialize</param>
        private void SerializeItemAndParent(Item item)
        {
            Sitecore.Diagnostics.Log.Info(String.Format("Serializing item: {0}", item.Paths.FullPath), this);
            Manager.DumpItem(item);
            // while parent is not null, we serialize the parent
            if(item.Parent != null)
                SerializeItemAndParent(item.Parent);
        }

        /// <summary>
        /// Method to check whether the item should be serialized, setting in config file has the paths that should be serialized
        /// </summary>
        /// <param name="item">item to check for serialization</param>
        /// <returns>true/false whether the item should be serialized</returns>
        private bool SerializeMe(Item item)
        {
            string serializePathsForDevelopment =
                Sitecore.Configuration.Settings.GetSetting("SerializePathsForDevelopment", "");
            foreach(string serializePath in serializePathsForDevelopment.Split(new string[]{"|"}, StringSplitOptions.None))
            {
                if(item.Paths.FullPath.ToLower().Contains(serializePath.ToLower()))
                {
                    return true;
                }
            }
            return false;
        }
    }

There you go, now you have automatic item serialization. This is one step of the way for sharing items among developers, they still need to commit to your source control, and they also need to update their solutions when working.

I´ve attached the eventhandler code and the include file.

SerializeEventHandler.config (1.04 kb)

SerializeEventHandler.cs (2.20 kb)