Wednesday, 9 April 2014

Get list of sublayouts or renderings used by item in Sitecore

Leave a Comment
Below function GetListOfSublayouts(string itemId) can be used to get list of sublayouts or renderings used by item in Sitecore. This function takes Sitecore item id and returns list of sublayouts/renderings used by a particular item in Sitecore 
public RenderingReference[] GetListOfSublayouts(string itemId)
        {
            Sitecore.Layouts.RenderingReference[] renderings=null;
            if (Sitecore.Data.ID.IsID(itemId))
            {
                 Item item = Sitecore.Context.Database.GetItem(Sitecore.Data.ID.Parse(itemId));
                 if (item != null)
                 {
                     renderings = item.Visualization.GetRenderings(Sitecore.Context.Device, true);                                      
                 }
            }
            return renderings;
        }
Below function DoesItemHasSublayout(string itemId, string sublayoutId) can be used to check whether a particular sublayout or rendering has been added to specific Sitecore item or not. This function takes Sitecore item id and Sublayout Id or Rendering Id and returns true/false.
 public bool DoesItemHasSublayout(string itemId, string sublayoutId)
        {
            if (Sitecore.Data.ID.IsID(itemId) && Sitecore.Data.ID.IsID(sublayoutId))
            {
                Item item = Sitecore.Context.Database.GetItem(Sitecore.Data.ID.Parse(itemId));
                if (item != null)
                {
                    if ((item.Visualization.GetRenderings(Sitecore.Context.Device, true).Where(r => r.RenderingID == Sitecore.Data.ID.Parse(sublayoutId))).Any())
                    {
                        return true;
                    }
                }
            }
            return false;
        }
Comments and suggestions are most welcome. Happy coding!

0 comments :

Post a Comment