Hiding the rootnode in a breadcrumb control (sitemap path)

Problem
A SiteMapPath control in asp.net reads its data directly from the SiteMapProvider which makes setting the ShowRootNode property impossible.
This maybe something you´re used to from menu controls and such that uses a SiteMapDataSource

Example
Home / Site 1 / SubSite 1 / Page below subsite 1

Here one might want to hide “Home” (which is the rootnode of the provider). 

Solution
You can, through code, check if the node that is to be rendered is a root node and if such hide it, as this example code shows:

Listen to the event when the nodes are created (ItemCreated).

    <asp:SiteMapPath ID="siteMapPath" runat="server"
        Pathseparator="/"
        OnItemCreated="SiteMapPath_ItemCreated">
        
    <NodeTemplate>
        <a href='<%# Eval("url") %>'><%# Eval("title") %></a>
    </NodeTemplate>
    
    <CurrentNodeTemplate>
        <%# Eval("title") %>
    </CurrentNodeTemplate>    
    
    </asp:SiteMapPath>

Check if the node is a rootnode and hide it. (also hide the first separator, it won´t be nice to start of with a slash).

protected void SiteMapPath_ItemCreated(object sender, SiteMapNodeItemEventArgs e)
{
    if (e.Item.ItemType == SiteMapNodeItemType.Root || (e.Item.ItemType == SiteMapNodeItemType.PathSeparator && e.Item.ItemIndex == 1))
    {
        e.Item.Visible = false;
    }
}

 

After this “fix” the rootnode is hidden in the breadcrumb.

Example
Site 1 / SubSite 1 / Page below subsite 1

…and yes, it also works in SharePoint since that platform is build on top of asp.net… ;-)

About these ads

, ,

  1. #1 by nc1943 on January 9, 2010 - 00:41

    If your interested, I posted an alternate non-code solution at: http://stackoverflow.com/questions/1050610/how-can-i-hide-the-sitemappath-root-node-on-home-page

  2. #2 by Not the same thing on July 17, 2010 - 00:52

    @Nc1942: Nope, your code hides the entire SiteMapPath if current page is a home page. Author is showing how to stop displaying only the root node, but render all other nodes.

  1. 2010 in review « Johan Leino

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

Follow

Get every new post delivered to your Inbox.

Join 313 other followers

%d bloggers like this: