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 Trott | C# ASP.Net MVC | October 6, 2010

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.

Was this article helpful to you?
 

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

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?

We respect your privacy, and will not make your email public. Learn how your comment data is processed.