How To Create A Breadcrumb in Episerver

In order to create a breadcrumb, you will need to be aware of the GetAncestors method.

Get Ancestor will return a list of pages from the users current page in the page tree, back to the start page. A few code examples using GetAncestors:

public static IEnumerable<PageData> GetAncestors(this PageData page)
{
return page.GetAncestors(PageReference.RootPage);
}

or

public static IEnumerable<IContent> GetAncestors(ContentReference contentReference)
{
var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
return contentRepository.GetAncestors(contentReference);
}

Moving back to the breadcrumb, we will also need to use the CompareToIgnoreWorkID(ContentReference) method so while we iterate back up, the page tree we know when we have hit the start page.

CompareToIgnoreWorkID compares a ContentReference to another instance.  One word of warning about CompareToIgnoreWorkID, is that it only compares against a content ID and the ProviderName for equality (there is also a similar Epi API call, called Equals that compares against content ID, ProviderName and WorkID.

private static List<PageReference> FindAllAncestors(ContentReference contentLink, IContentLoader contentLoader)
{
var path = contentLoader.GetAncestors(contentLink)
.Reverse()
.SkipWhile(x => ContentReference.IsNullOrEmpty(x.ParentLink) || !x.ParentLink.CompareToIgnoreWorkID(ContentReference.StartPage))
.OfType<PageData>()
.Select(x => x.PageLink)
.ToList();
return path;
}

Working Code Example

All the above code can be downloaded in a fully working wesite from my github account here JonDJones.Com.EpiBreadcrumbs

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

More Posts

2 replies
  1. Steven
    Steven says:

    Thanks for the tutorial. I implemented it just find on our CMS side, but I am having difficulties with the commerce side of things since I can’t create a commerce object of type PageData. Is there any thoughts you can provide about doing that?

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *