Tuesday, 8 April 2014

How to check Sitecore item presentation details

3 comments
Sometimes; I've to find out whether a given Sitecore item has presentation details set or not. I've to write business logic that depends if Sitecore item has any layout associated with it. I've written a function DoesItemHasPresentationDetails(string itemId) which takes Sitecore item id and returns true/false based on Sitecore Item presentation details.
public bool DoesItemHasPresentationDetails(string itemId)
        {
            if (Sitecore.Data.ID.IsID(itemId))
            {
                Item item = Sitecore.Context.Database.GetItem(Sitecore.Data.ID.Parse(itemId));
                if (item != null)
                {
                    return item.Fields[Sitecore.FieldIDs.LayoutField] != null
                           && !String.IsNullOrEmpty(item.Fields[Sitecore.FieldIDs.LayoutField].Value);
                }
            }
            return false;
        }
Code explanation:
  • Sitecore.Data.ID.IsID() : This method takes in a string guid and returns true if it's a valid guid in Sitecore . The string must be 38 characters long, start with a curly brace "{" and end with a curly brace "}".
    For example :
    string itemId = "{AD1495DB-36FC-4B7A-80B6-D6170AB7F3B1}";
  • Sitecore.Data.ID.Parse() : This method takes in a string guid and returns a Sitecore ID object.
  • Sitecore.Context.Database.GetItem() : This method takes in a Sitecore ID object and return a Sitecore Item object.
  • Sitecore.FieldIDs.LayoutField : Sitecore.FieldIDs.LayoutField is a constant that hold the value of the __Renderings field ID.
Above function will return true if presentation details has been set like below image:

Above function will return false if presentation details has been set like below image (no layout):


Alternatively, you can use DoesItemHasLayout(string itemId) function to check whether Sitecore item has layout associated with it or not. This function takes Sitecore item id and returns true/false.
public bool DoesItemHasLayout(string itemId)
        {           
            if (Sitecore.Data.ID.IsID(itemId))
            {
                Item item = Sitecore.Context.Database.GetItem(Sitecore.Data.ID.Parse(itemId));
                if (item != null)
                {
                    LayoutItem layoutItem = item.Visualization.GetLayout(Sitecore.Context.Device);
                    if(layoutItem!=null)
                    {
                        return true;
                    }
                }
            }
            return false;
        }    
Comments and suggestions are most welcome. Happy coding!

3 comments :


  1. Wie kannst du deine IT-Karriere in München auf das nächste Level heben? Die Antwort könnte in einer gezielten cybersecurity weiterbildung liegen, die nicht nur dein technisches Wissen erweitert, sondern auch deine Karrierechancen erheblich verbessert. In der bayrischen Metropole bieten zahlreiche Unternehmen und Institutionen spezialisierte Programme an, die dir helfen können, dich als Experte im Bereich Cybersicherheit zu etablieren. Ein solches Programm ist das csvisor, das neben fundierten Kenntnissen auch ein effektives Karriere-Kompass Coaching bietet. So bist du bestens gerüstet für die Herausforderungen und Chancen des digitalen Zeitalters.

    ReplyDelete