Breadcrumb in Sitecore

In my quest for providing simpler topics about Sitecore development, I have decided to give you one of my simplest XSLT’s: the breadcrumb. This breadcrumb is probably used in every project I have ever implemented.
To create a breadcrumb you must read the current item, and all of it’s parents untill a you reach the front page and list them in the order from the front page and downwards to your own page. To do this, you must use the ancestor-or-self axis. This axis returns all descendants of your page and the page itself.
Here is the complete code. This is all you need:

<xsl:variable name="navigationItems" select="$sc_currentitem/ancestor-or-self::item&#91;sc:IsItemOfType('mypages',.) and sc:fld('ShowInMenu',.)='1'&#93;"/>
  <xsl:for-each select="$navigationItems">
      <xsl:if test="position()>1">
      <sc:link text="{sc:clip(sc:fld('MenuTitle',.),20,1)}" />

Here is how it works: First I use the ancestor-or-self axis to get all pages of a certain type (using sc:IsItemOfType(), see my blog on using this function) and with a certain property (using sc:fld(), see this blog on using sc:fld or sc:field).
The axis have done all the work for me. Now I can loop over each item and draw them in an unordered list. To seperate each of the items I draw a “>” but only if I am not drawing the first item (using position() > 1).
Notice how I draw the breadcrumb link. I use the sc:link() and clips the menu title (using sc:clip()) if the title is too long. 
You should replace “mypages“, “ShowInMenu” and “MenuTitle” with the appropriate values you use in your project.

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in Sitecore 6 and tagged , , , . Bookmark the permalink.

5 Responses to Breadcrumb in Sitecore

  1. Dharmendra says:

    Hey Great Work,
    But Could you please tell me which value we have to replace
    instead of these mypages“, “ShowInMenu” and “MenuTitle

    Thanks for your time

  2. briancaos says:

    The sc:IsItemOfType(‘mypages’,.) function returns all items in the axis that inherits from “mypages”. This is a way to filter all items that is not of a type that you do not wish to include in your breadcrumb. So here is a tip from Pentia’s agile Sitecore design ( Create a template with no fields on it. Name the template “WebPages”. Then let all of the pages that is a web page inherit from this. Then you can filter on this template in the menu, the breadcrumb etc. by adding the sc:IsItemOfType(‘WebPages’,.) caluse to the axis.

    “ShowInMenu” is a field I have created on my template. It is a checkbox, that will allow me to hide items from my breadcrumb. If the checkbox is not checked, the item is not shown in the breadcrumb.

    “MenuTitle” is also a field on my template. In this field I write the name of the item that I wish to have displayed in my breadcrumb, my menu etc. Why not use the ordinary page title? It is very common that menu titles are shorter than page titles.

  3. Rob Gray says:

    Thanks. Old article but a good help for a sitecore newbie

  4. Rob Gray says:

    Thanks. Old article but a good help for a sitecore newbie, like me.
    One change I made was to include to show the current page as just a field (no link).


  5. briancaos says:

    Now when you have read how to use XSLT in Sitecore, you should read my article on why not to use XSLT’s:
    Using UserControls instead of XSLT in Sitecore projects/

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s