Saturday, 24 May 2014

Serialize a content item programmatically in Sitecore

Leave a Comment
Below function SerializeItem(string itemId) will serialize a content item programmatically in Sitecore. This function takes item id as input parameter and will create a .item file in the default serialization directory.
public void SerializeItem(string itemId)
        {
            if (Sitecore.Data.ID.IsID(itemId))
            {
                Database masterDB = Sitecore.Configuration.Factory.GetDatabase("master");
                Item item = masterDB.GetItem(Sitecore.Data.ID.Parse(itemId));
                if (item != null)
                {
                    Sitecore.Data.Serialization.Manager.DumpItem(item);
                }
            }
        }
Sitecore.Data.Serialization.Manager class is having all Serialization related operations. DumpItem(item) function serializes only given item in default serialization directory. If you want to serialize entire content tree then you can use DumpTree(item) function.

DumpItem() and DumpTree() both these functions are overloaded functions. If you want to serialize item in any specific serialization directory instead of default serialization directory then you can pass directory path as input parameter along with Sitecore item to these functions.
Example:
Sitecore.Data.Serialization.Manager.DumpItem(@"E:\Inetpub\wwwroot\netsitecore", item);
Make sure that appropriate write access has been given to serialization directory else you will get access denied error.
Comments and suggestions are most welcome. Happy coding!

0 comments :

Post a Comment