Language: C#
Create a breadcrumb string list in EPiServer (used for page statistics or the actual breadcrumb)
/// <summary> /// Creates a string list that contains the hierrarchy leading to the current page /// </summary> /// <param name="currentPage"></param> /// <param name="ReplaceStartWithLangCode"></param> /// <param name="ReverseSort"></param> /// <returns></returns> public static List<string> GetBreadCrumb(PageData currentPage, bool ReplaceStartWithLangCode, bool ReverseSort) { if (currentPage != null) { PageData breadCrumbPage; List<string> PageNames = new List<string>(); // If current page is the startpage, insert the current language code instead of the PageName if (currentPage.PageLink.ID == PageReference.StartPage.ID) { if (ReplaceStartWithLangCode) PageNames.Add(currentPage.LanguageID); else PageNames.Add(currentPage.PageName.ToString().Replace("/", " ").Replace(" ", " ").Replace(" ", " ").Trim()); } else PageNames.Add(currentPage.PageName.ToString().Replace("/", " ").Replace(" ", " ").Replace(" ", " ").Trim()); PageReference parentReference = currentPage.ParentLink; // Loop backwards from current page up to the startpage and build a breadcrumb while (parentReference != PageReference.EmptyReference && parentReference != PageReference.RootPage) { breadCrumbPage = DataFactory.Instance.GetPage(parentReference); if (breadCrumbPage.PageLink.ID == PageReference.StartPage.ID) { // If current page is the startpage and ReplaceWithStringLang is true, insert the current language code instead of the PageName if (ReplaceStartWithLangCode) PageNames.Add(breadCrumbPage.LanguageID); else PageNames.Add(breadCrumbPage.PageName.ToString().Replace("/", " ").Replace(" ", " ").Replace(" ", " ").Trim()); } else PageNames.Add(breadCrumbPage.PageName.ToString().Replace("/", " ").Replace(" ", " ").Replace(" ", " ").Trim()); parentReference = breadCrumbPage.ParentLink; } // By default use standard order of the pages: /startpage/subpage1/subsubpage1 if(!ReverseSort) PageNames.Reverse(); return PageNames; } return new List<string>(); }
Tags:
Description:
How to create a breadcrumb string list in EPiServer that can be used for either page statistics (I used this piece of code for implementing Google Analytics on a site) or as the usual breadcrumb on your site.Usually clients don't want the startpage name as the string in the breadcrumb when you use it in statistics, because the name of the startpage can be changed and if that happends your statistics will be somewhat worthless. Instead we use the startpage's language code as the string, example: "/en/SubPage1/Page" instead of "/Welcome to our Site/SubPage1/Page".
Report Abuse
Subscribe
Discuss
What's new
What is it
New Snippet
Recent Snippets
My Snippets
Web Code
Search

