Programmatically Modify Site-Map Nodes in Memory

I usually go on holiday over the festive season and whenever I do people always ask me to put the pictures I take online. So to keep them all happy I've been adapting the personnal starter kit's photo album and have stumbled on a really annoying problem with the sitemap feature. The sitemap xml file doesn't allow duplicate urls.

The reason this is annoying is crystal clear in my mind but might be a bit long winded to explain...

Picture the following breadcrumb and page navigation

Albums -> Place -> Photos 

albums.aspx -> place.aspx?place=1 -> photos.aspx?album=8

The reason this is annoying is crystal clear in my mind but might be a bit long winded to explain...

Picture the following breadcrumb and page navigation

Albums -> Place -> Photos 

albums.aspx -> place.aspx?place=1 -> photos.aspx?album=8

The reason this is annoying is crystal clear in my mind but might be a bit long winded to explain...

Picture the following breadcrumb and page navigation

Albums -> Place -> Photos 

albums.aspx -> place.aspx?place=1 -> photos.aspx?album=8

This is no problem as the navigation is linear like the breadcrumb control. Each url is unique and can be put into a site map xml file as long as the urls are resolved with the supplied querystrings*. However, what if you want to change 'Place' in the breadcrumb with the name of an actual place.

Albums -> US -> Photos 

albums.aspx -> us.aspx -> photos.aspx?album=8

Albums -> UK -> Photos 

albums.aspx -> uk.aspx -> photos.aspx?album=9

You can replace page.aspx with custom urls easily with Url Rewriting but, and here it is, doing this essentially creates a fork in the navigation, albums to uk or us back to photos. The fork means you can not represent this navigation in a sitemap because you need a sitemapnode for the 'US' and one for the 'UK' and both of these would need a photo sitemapnode child, and that means there would be two photos.aspx urls.

I tried all sorts of approaches to try getting around this problem and then I came across this article.....

How to: Programmatically Modify Site-Map Nodes in Memory 

.... although the article covers the ins and outs of solving this problem it doesn't tell you the best way to do it. The article recommends that you handle the SiteMapResolveEventHandler in the page but this only works if the page is the last item in the breadcrumb, i.e. the breadcrumb will change to 'UK' on uk.aspx however that breadcrumb node will change to 'Page' again when photos.aspx is visited.

In order to get around this you need to apply the breadcrumb change throughout the whole site and that means handling SiteMapResolveEventHandler in the Global.asax and mapping the event handler in the Application_Start event.

In here you need check the current url and decide what sitemapnode to change and what title and url to change it too. This gets rather long winded as a change on Place.aspx involves working with the current site map node while a change on Photos.aspx involves changing the current site maps nodes parent.

I'm still trying to get my head around all this but there appears to be quite a bit more to site map navigation than I had originally thought, and it's all because sitemap files don't handle duplicate urls!!

*resolving a url allows you to keep the sitemapnode's url containing it's querystrings in the breadcrumb control. For example your sitemapnode may have a url of search.aspx however the actual url used would be search.aspx?value=1. If you didn't resolve your url in this case then the breadcrumb would point to the search.aspx file, as that how it appears in the sitemap file, and would disregard the querystring.

Published Thursday, November 09, 2006 5:13 PM by dsmyth
Filed under:

Comments

No Comments