Custom SiteMap Provider

Right before I went down with an illness a few weeks back I started working with the navigation controls in ASP.NET 2.0.  I actually started writing this entry about three weeks ago, so bear that in mind too.  I really wanted something that would be able to respond to any new page additions at the time they are added or changed.  I did not want to have to create a sitemap file and write some code to update it, I really hate messing with XML to be honest.

So I did some searching and found several entries for a SQL Site Map Provider, a custom SiteMapProvider.  Thanks to a pretty good article on MSDN and a good rehash by Jeff Prosise, I was on my way.  I did find some things to be pretty annoying to me, mostly because something was not clearly documented about the menu control and the other was the sample code was in C#, and I like VB.

Here are some general things I learned about SiteMap Providers, you can create your own, you should always set your DisplayDynamicMenuLevel=2 for the Menu Control and the site map will not let you have multiple entries for the same URL.  I also learned about SQLCacheDependancy, which allow you to 'watch' a database table for changes.  So here are the pieces you will need to create your own custom SQLSiteMapProvider:

  • SiteMap Table
  • SQLCacheDependancy pointing to the SiteMap Table
  • Configure your Web.Config for your Custom SiteMap Provider and SQl Dependancy 
  • Your custom SiteMap Provider class

This is the code for my SiteMap Table, as you can see I have added a few fields from the examples.

CREATE TABLE [dbo].[SiteMap] (
 [ID] [int] IDENTITY (1, 1) NOT NULL ,
 [ParentId] [int] NOT NULL ,
 [Title] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
 [Description] [varchar] (512) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
 [Keywords] [varchar] (512) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
 [Url] [varchar] (512) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
 [Roles] [varchar] (512) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
 [PageOrder] [int] NULL ,
 [IsVisible] [bit] NULL ,
 [Disable] [bit] NULL ,
 [IconFile] [varchar] (512) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
 [Image] [varchar] (512) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
 [Active] [bit] NOT NULL

You can review the attached SQL script for more details and my basic CRUD stored procedures.  Some of the fields were borrowed from DotNetNuke, such as the IsVisible and Disabled fields.  These are not supported by the Navigation Controls and the SiteMap Provider, so you will have to work that one out yourself.

I plan on going into more depth real soon on both the SQLSiteMapProvider and the SQL Dependancies.  As I write this I am still using the customer SiteMapProvider, but I think because of the two restrictions I listed above I will have to abandon them to go forward to meet the requirements of my clients.  So another Blog entry to follow on that.

Share This Article With Your Friends!