Sunday, September 7, 2008

Building and Consuming a Dynamic Sitemap in ASP.NET 2.0

By Thomas Kurek

You need to build a dynamic sitemap right from a dataset because you don't have static content on your website

Introduction
Sitemaps and breadcrumbs (SiteMapPath) are fantastic. For a dynamic site, they can be crucial, since search engines are historically agnostic to QueryString driven content that is common for dynamic websites. A sitemap can help search engines find this content and index it appropriately, expose breadcrumbs for users, and help users see in one place everything you have to offer them.

The out-of-the-box functionality for Sitemaps with ASP.NET 2.0 is fantastic. Many of the complexities previously associated with them have been addressed; however, while implementing one for our system we faced a number of challenges which were not addressed by Microsoft.

My code and approach in this article will help you if you share any of the following motivations with me:

You are using a hierarchical DataSet to generate the tree for your dynamic content which would feed a sitemap
You want to generate the XML needed for the ASP.NET SiteMap datasource straight from the DataSet in a fast way that minimizes lines of code
Your content management system is large enough to vote against simply generating this DataSet on every web request (remember it feeds breadcrumbs on all your pages) or filling up your server cache with this huge object that represents all the content in your system - thus you want a static XML SiteMap file that is updated every day (or at a frequency that you define)
You want to backup your XML SiteMap files for maintenance or just safety
You have multiple content sections in your dynamic CMS that are different trees - meaning your pages are context sensitive and would feed off of separate sitemaps in the same web application
You want to learn how to do an XSLT in memory in .NET 2.0
You want to learn how to use embedded resource files in .NET 2.0
You just want to see the XSLT that can turn a hierarchical DataSet into a *.sitemap file
You want to see how to handle identical Urls in the sitemap (which is forbidden by the out-of-the-box SiteMapProvider)
Step 1: Identify your naming convention
To begin, you must define your naming convention for your XML file. As this is an automated process you want to shield the client from thinking of its inner workings. In my case, I needed 3 components to uniquely identify my sitemaps:

ApplicationID (the ID for the web application of interest)
CultureName (the culture name for the current content i.e. en-US or en-GB)
SitemapType (the content tree that uniquely identifies groups of content for separation)

I have the following Fields in my Sitemap class:

More...
http://www.codeproject.com/KB/aspnet/DynamicSitemapASPNET2.aspx

ASP.Net Feeds