Thursday 26 June 2014

How to add a sublayout to Sitecore item programmatically

Leave a Comment
Today I've noticed a post in Sitecore SDN forum regarding how to add a sublayout to multiple items programmatically. Below function AddSublayoutToItem(string itemId, string sublayoutId) takes Sitecore item id and Sublayout id as input parameter and add specific sublayout to Sitecore item. I’ve added inline comments in below code to make it more understandable. Call this function iteratively for adding sublayout to multiple Sitecore items.
  public void AddSublayoutToItem(string itemId, string sublayoutId)
        {
            using (new Sitecore.SecurityModel.SecurityDisabler())
            {
                if (Sitecore.Data.ID.IsID(itemId) && Sitecore.Data.ID.IsID(sublayoutId))
                {
                    //Get the master database and get the item on which you want to add sublayout
                    Database masterDatabase = Database.GetDatabase("master");
                    Item item = masterDatabase.GetItem(Sitecore.Data.ID.Parse(itemId));

                    //  Or you can also get Sitecore Item from Context Database as per your requirement
                    //  Item item = Sitecore.Context.Database.GetItem(Sitecore.Data.ID.Parse(itemId));

                    if (item != null)
                    {
                        // Get the layout definitions and the device definition
                        LayoutField layoutField = new LayoutField(item.Fields[Sitecore.FieldIDs.LayoutField]);
                        LayoutDefinition layoutDefinition = LayoutDefinition.Parse(layoutField.Value);
                        DeviceDefinition deviceDefinition = layoutDefinition.GetDevice(Sitecore.Context.Device.ID.ToString());

                        //Create a RenderingDefinition and add the reference of sublayout or rendering
                        RenderingDefinition renderingDefinition = new RenderingDefinition();
                        renderingDefinition.ItemID = sublayoutId;
                        //Set placeholder where the rendering should be displayed
                        renderingDefinition.Placeholder = "content"; 
                        // Set the datasource of sublayout, if any
                        renderingDefinition.Datasource = "{24240FF2-B4AA-4EB2-B0A4-63E027934C38}";

                        // you can also set datasource of sublayout using Sitecore Path
                        // renderingDefinition.Datasource = "/sitecore/content/Home/Books";

                        //Add the RenderingReference to the DeviceDefinition
                        deviceDefinition.AddRendering(renderingDefinition);

                        // Save the layout changes
                        item.Editing.BeginEdit();
                        layoutField.Value = layoutDefinition.ToXml(); ;
                        item.Editing.EndEdit();
                    }
                }
            }
        }
Note: This code is tested on Sitecore 7.2 version. Comments and suggestions are most welcome. Happy coding!

0 comments :

Post a Comment