Wednesday, 23 April 2014

Get data source of sublayout or rendering in Sitecore

Leave a Comment
Today I noticed a post in Sitecore SDN forum regarding how to retrieve data source of sublayout or rendering for an item in Sitecore. Several sublayouts or renderings have been added in Sitecore item presentation details. We need to get data source of each sublayout.

First of all we need to get list of sublayouts or renderings used by item in Sitecore. Below function GetListOfSublayouts(string itemId) can be used to get list of sublayouts or renderings used by 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;
        }
After getting list of sublayouts, iterate through each sublayout and retrieve data source value by using rendering.Settings.DataSource. Below function GetListOfDataSource(RenderingReference[] renderings) takes renderings as input parameter and return list of data source id.
RenderingReference[] renderings = GetListOfSublayouts(itemId);
List<string> ListOfDataSource = GetListOfDataSource(renderings);
public List<string> GetListOfDataSource(RenderingReference[] renderings)
        {
            List<string> ListOfDataSource = new List<string>();
            foreach (RenderingReference rendering in renderings)
            {
                ListOfDataSource.Add(rendering.Settings.DataSource);
            }
            return ListOfDataSource;
        }
Comments and suggestions are most welcome. Happy coding!
Read More...

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

Tuesday, 8 April 2014

How to check Sitecore item presentation details

Leave a Comment
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!
Read More...

Saturday, 5 April 2014

Get Media Item File Size in Sitecore

10 comments
I recently got into a situation where I have to return the size of selected pdf file. In Sitecore using Sitecore.Data.Items.MediaItem class, we can access file size, extension, mime type etc of selected media item.

I have written a function GetSitecoreMediaItemSizeInBytes(string itemId) which takes item id of media item and return the size of media item in bytes.
public long GetSitecoreMediaItemSizeInBytes(string itemId)
        {
            long mediaItemSize = 0;
            if (Sitecore.Data.ID.IsID(itemId))
            {
                Item sitecoreItem = Sitecore.Context.Database.GetItem(Sitecore.Data.ID.Parse(itemId));
                if (sitecoreItem != null)
                {
                    MediaItem mediaItem = new MediaItem(sitecoreItem);
                    mediaItemSize = mediaItem.Size;                   
                }
                else
                {
                    Sitecore.Diagnostics.Log.Error("Could not find given sitecore media item with id '" + itemId + "' ", this);
                }
            }
            return mediaItemSize;
        }
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.
  • MediaItem mediaItem = new MediaItem(sitecoreItem) : Using this piece of code, we are getting Sitecore Media Item object.
  • mediaItem.Size : Returns size of media item in bytes.
  • mediaItem.Extension : Returns file extension of media item. In my case; it was .pdf.
  • mediaItem.MimeType : Returns Mime Type of media item. In my case; it was application/pdf.
Comments and suggestions are most welcome. Happy coding!
Read More...