Kloojed

Simple breadcrumb using the PageSiteMapProvider

A pretty common thing to implement on a web site is a breadcrumb and with EPiServers PageSiteMapProvider it's an easy task.

In this tutorial I assume you have a clean install of EPiServer CMS 5.

The first thing we need to do is enable the PageSiteMapProvider in our web.config by adding the following xml.

  1. <siteMap defaultProvider="PageSiteMap">
  2. <providers>
  3. <add name="PageSiteMap" type="EPiServer.PageSiteMapProvider, EPiServer" />
  4. </providers>
  5. </siteMap>

If we were lazy we could use the the asp:SiteMapPath and call it a day and the result would be OK but i'm a fan of clean HTML and therefore I decide we make a custom control that renders exactly what we want.

So let's start by creating a new class named e. g. Path.

  1. public class Path : Control {
  2. protected override void CreateChildControls() {
  3. if(SiteMap.CurrentNode == null)
  4. return;
  5. PrependText(SiteMap.CurrentNode);
  6. PageSiteMapNode parent = SiteMap.CurrentNode.ParentNode as PageSiteMapNode;
  7. while (parent != null && parent != SiteMap.RootNode && parent.CurrentPage.PageLink != PageReference.RootPage) {
  8. Controls.AddAt(0, new LiteralControl(" / "));
  9. PrependAnchor(parent);
  10. parent = parent.ParentNode as PageSiteMapNode;
  11. }
  12. base.CreateChildControls();
  13. }
  14. public virtual void PrependText(SiteMapNode node) {
  15. Controls.AddAt(0, new LiteralControl(HttpUtility.HtmlEncode(node.Title)));
  16. }
  17. public virtual void PrependAnchor(SiteMapNode node) {
  18. HtmlAnchor anchor = new HtmlAnchor { HRef = node.Url, InnerText = node.Title };
  19. Controls.AddAt(0, anchor);
  20. }
  21. }

For my simple example i'm quite satisfied and ready to test my new breadcrumb control. To do that we just need to add our newly created Path to our page like this.

  1. <Kloojed:Path runat="server" />

We should now have a clean breadcrumb with no extra markup generated from the included PageSiteMapProvider.

  • Posted December 4, 2008
  • Nice! I'll give it a try.

  • pappabj0rn:

    Yeah go ahead and spice it up a little bit :)

  • I'm new with EPiServer,

    Can you please give more explanation about where do I have to insert <siteMap defaultProvider="PageSiteMap"> </siteMap>

    and do I have to add a new template to insert a class that you providing?

    • Posted by Viktor
    • June 3, 2009
  • Hi Viktor and welcome to the EPiServer family!

    Just add the siteMap section directly after <system.web> in your web.config, you don't need to add a new template to use the provided class. Just create a new class, paste my example code and compile your project. Then you need to add something like this in your web.config:

    1. <pages validateRequest="false" enableEventValidation="false">
    2. <controls>
    3. <add tagPrefix="Kloojed" namespace="your namespace" assembly="your assemblyname" />
    4. </controls>
    5. </pages>

    That should be all, good luck!

  • Love it! Exactly what I needed. Good work :)

    • Posted by Mattias
    • August 31, 2009
  • Mattias:

    I'm glad you liked it and thanks for the feedback!

    • Posted by Marcus Lindblom
    • August 31, 2009

Leave a comment


Information

About

Marcus Lindblom is a Swedish professional frontend engineer with specialities like Web standards, ASP.NET, EPiServer, accessibility and long experience in developing high end websites for the public sector and commercial companies.

Copyright

Copyright © 2008 Marcus Lindblom. Powered by StormBreaker - Standards Compliant CMS. Hosted by Meridium AB.