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.
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 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 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!

edirne
ReplyDeletetrabzon
adana
yozgat
XXCFF
A49BA3E8EC
ReplyDeletekiralık hacker
hacker arıyorum
belek
kadriye
serik