Hiding the Root Node from SiteMapPath

Jeff Prosise made a nice blog entry about hiding the root node in the SiteMapPath-control. Some people, like myself, experienced an issue when the current node and the root node ar actually the same. Somehow the check that should avoid that issue fails. I believe because the current node is in fact a clone of the current node, and as such not equal to the current node (i.e. root node) itself. To get around this, I have changed the code somewhat to do the check on the actual nodes. The body of the HideRootNode-method is now as follows:

// Return root node in case this is the root.
if(SiteMap.RootNode == SiteMap.CurrentNode) return SiteMap.CurrentNode;

// This is not the root node, so rebuild path without root.
SiteMapNode node = SiteMap.CurrentNode.Clone();
SiteMapNode current = node;

while (node.ParentNode != SiteMapNode.RootNode)
    node.ParentNode = node.ParentNode.Clone();
    node = node.ParentNode;
node.ParentNode = null;
return current;

Thanks to Jeff for his original insight. It saved me a lot of time.

2 thoughts on “Hiding the Root Node from SiteMapPath

  1. Leon Zandman

    Your code does not compile. SiteMapNode.CurrentNode doesn’t exist. I guess it should be SiteMap.CurrentNode.

    Also the variable named "root" isn’t declared in your code…


Leave a Reply

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

9 + 1 =