Extending web.sitemap

If you are using ASP.NET you are probably already familiar with the new sitemap components.

The sitemapdatasource and web.sitemap elements make it very easy to build and maintain rich site structures.

Using the Eval statement and recursing across the siteMapNodes is a very powerfull yet simple mechanism which allows for some low level control of site navigation control rendering. Using a repeater control with '<%# Eval("title") %>' allows you to render the details as you want it.

Problem comes when you want to extend the web.sitemap with some custom information. Using Eval throws an error as it appears to be constrained by schema on the web.sitemap file. This schema only allows a finite set of attributes.

So suppose you want to have a custom attribute called icon which gets pulled into your navigation generation code to build a nice navi bar. The web.sitemap would look like:

<siteMapNode url="Default.aspx" title="Events"  description="Click here for event details" icon="Images/events.png"/>

There are two choices at this point:

1. Build your own navigation provider with derives from StaticSiteMapProvider and override the required node navigation controls. This is straight forward but in some cases may be more work than you would like.

2. Rather than use an Eval use:

 '<%# ((SiteMapNode)Container.DataItem)["icon"] %>'

For adding a few custom attributes choice 2 this seems a good solution. If your web.sitemap requirements include some changes to the node structure then you should implement a custom provider.

Author Mark Page

Mark is trying not to drink too much 'cool aid' at the moment. All the more for everyone else!

Add Comment


Your comment has been received and will be shown once it passes moderation.