Monday, May 18, 2015

Breadcrumbs for pages in Sitecore

Breadcrumbs will generally start with the home page followed, by each level of the site architecture and ending in the current item. Therefore in this example, the breadcrumbs will always have the home item (linked to the home page) and the current item (with no link). The logic required is filling in the missing pieces (the structure in between home and current item).
Item currentItem = Sitecore.Context.Item; // Current sitecore item
BreadCrumbs.Current = currentItem.Fields["Title"].ToString(); // set current items title on breadcrumb object
currentItem = currentItem.Parent; // go to parent of current item

while (currentItem.ID != new ID(Items.ContentRootItem))
    // add parent item to breadcrumb object (a list of parent items)
    BreadCrumbs.Parents.Add(new Common.MenuItem
        Link = Sitecore.Links.LinkManager.GetItemUrl(currentItem),
        Text = currentItem.Fields["Title"].ToString()

    currentItem = currentItem.Parent; // get parent of current item

BreadCrumbs.Parents.Reverse(); // reverse order of parents to get in correct order
/// <summary>
/// Menu Item
/// </summary>
public class MenuItem
    /// <summary>
    /// Gets or sets the link value.
    /// </summary>
    public string Link { get; set; }

    /// <summary>
    /// Gets or sets the text value.
    /// </summary>
    public string Text { get; set; }

    /// <summary>
    /// Gets or sets the css class value.
    /// </summary>
    public string CssClass { get; set; }
In the code above there is a simple breadcrumb object which contains a list of parent items and the current item's title. On the front end you simply output the home item in the sublayout, set the current item's titleand using a repeater, loop through the parent items to display their title as a hyperlink. Items.ContentRootItem is a resources file string containing the GUID for the root item, we are looping through the parents until we hit this item.


  1. Hi may i know the references you are using in this code

    1. The following references are used:

      using Sitecore.Data;
      using Sitecore.Data.Items;
      using System;
      using System.Collections.Generic;
      using System.Data;
      using System.Linq;

    2. Hi ryan follow up question i still can't resolve the issue on Items.ContentRootItem and Common.MenuItem. I've tried hardcoding the id of home item but i still can't resolve the issue on Common.MenuItem

      Thanks for your help

    3. Items.ContentRootItem is the GUID of the parent item (Home) in Sitecore, I am storing it inside a resources file as a string.

      The post has been updated to contain the menu item class.