Extending the ASP.Net Web.Sitemap Xml Document

A look at the ASP.Net web.sitemap document for website navigation and see how extending the web sitemap by binding to custom attributes.

By Tim TrottC# ASP.Net MVC • October 6, 2010
Extending the ASP.Net Web.Sitemap Xml Document

The web.sitemap document is an XML format that contains the navigation structure for your site and is used by search engines to discover content on your website. Various ASP.Net controls use the document to render navigation elements such as breadcrumb trails, navigation menus and trees.

While the standard format may be adequate for most people's needs, some may require additional information to be stored with the page link. This could be the target for the link, a product id, a different description or anything else.

The beauty of the XML format is that it is extensible. That means that it is very easy to add extra information to it.

Let's have a look at a sample sitemap from the previous tutorial.

xml
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
    <siteMapNode url="Default.aspx" title="Home"  description="This is our homepage">
      <siteMapNode url="about.aspx" title="About Us"  description="This page is all about our company" />
      <siteMapNode url="products.aspx" title="Products"  description="This is our main product page" >
        <siteMapNode url="product1.aspx" title="Sample Product 1"  description="This is a sample product" />
        <siteMapNode url="product2.aspx" title="Sample Product 2"  description="This is a sample product" />
      </siteMapNode>
      <siteMapNode url="services.aspx" title="Our Services"  description="These are the services we provide" />
      <siteMapNode url="contact.aspx" title="Contact Us"  description="How to contact us">
        <siteMapNode url="map.aspx" title="How to Find Us"  description="A Map of how to find us" />
      </siteMapNode>
    </siteMapNode>
</siteMap>

Each SiteMapNode has three elements. A URL, title and description. To add a product id to the nodes is as easy as changing the XML, so the two products become:

xml
        <siteMapNode url="product1.aspx" title="Sample Product 1"  description="This is a sample product" prodID="PROD001" />
        <siteMapNode url="product2.aspx" title="Sample Product 2"  description="This is a sample product" prodID="PROD002" />

Now all we need is some way of getting this value on the products page. The easiest way is to use the SiteMap class indexer to access the attributes:

C#
string prodID = SiteMap.CurrentNode["prodID"]

In the previous tutorial, we saw how to add sitemap information to the aspx page using an eval:

xml
<%= SiteMap.CurrentNode.Title %>

And you can do the same thing with the attribute as well.

xml
<%= SiteMap.CurrentNode["prodID"] %>

Easy. Now for the harder part. Binding new attributes to existing navigation controls. For this, we need to create a function to attach to the DataBound event of a control. In this example, I am going to append the prodID to the end of the title on the TreeView control. This method can be adapted for use with any of the navigation controls and properties.

First, start with a basic sitemap navigation tree view and data source and link the OnTreeNodeDataBound event to a function called TreeView1_TreeNodeDataBound.

xml
<asp:TreeView ID="TreeView1" runat="server" DataSourceID="SiteMapDataSource1" OnTreeNodeDataBound="TreeView1_TreeNodeDataBound" >
</asp:TreeView>
<asp:SiteMapDataSource ID="SiteMapDataSource1" Runat="server"></asp:SiteMapDataSource>

In the C# code behind, create a function called TreeView1_TreeNodeDataBound with the following code.

C#
protected void TreeView1_TreeNodeDataBound(object sender, TreeNodeEventArgs e)
{
    SiteMapNode node = e.Node.DataItem as SiteMapNode;
    e.Node.Text = string.Format("{0} - {1}", node.Title, node["prodID"]);
}

This illustrates how to access the default properties and custom attributes for the sitemap nodes and how to bind them to the custom properties of a control.

About the Author

Tim Trott is a senior software engineer with over 20 years of experience in designing, building, and maintaining software systems across a range of industries. Passionate about clean code, scalable architecture, and continuous learning, he specialises in creating robust solutions that solve real-world problems. He is currently based in Edinburgh, where he develops innovative software and collaborates with teams around the globe.

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

My website and its content are free to use without the clutter of adverts, popups, marketing messages or anything else like that. If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

There are no comments yet. Why not get the discussion started?

New comments for this post are currently closed.