CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

Create a breadcrumb string list in EPiServer (used for page statistics or the actual breadcrumb)

553 Views
Copy Code Show/Hide Line Numbers
/// <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>();
}
by rawbert
  December 11, 2009 @ 7:45am
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".

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate