Building a Custom Site Map Provider
The article explains how to build a simple custom site map provider.
Introduction
In the last ASP.NET course I delivered, I was asked how to build a custom site map provider.
This article holds the answer. You can download the full example from here.
Building a Custom Site Map Provider
There are times when our requirements demand that our site navigation will not be based on the default site map provider.
Such times are, for example, when we want to use a database table to hold our site navigation. During these times, we can create a custom site map provider. What we need to do is to inherit from the StaticSiteMapProvider
class and then implement our business functionality. There are two methods that we need to override which are BuildSiteMap
and GetRootNodeCore
. The first is building the site map and the second returns the root node of the built site map.
The following code demonstrates how to create a simple site map provider:
using System.Web;
namespace WebApplication1
{
public class CustomSiteMapProvider : StaticSiteMapProvider
{
#region Members
private readonly object _siteMapLock = new object();
private SiteMapNode _siteMapRoot;
#endregion
#region Methods
public override SiteMapNode BuildSiteMap()
{
// Use a lock to provide thread safety
lock (_siteMapLock)
{
if (_siteMapRoot != null)
{
return _siteMapRoot;
}
base.Clear();
CreateSiteMapRoot();
CreateSiteMapNodes();
return _siteMapRoot;
}
}
protected override SiteMapNode GetRootNodeCore()
{
return BuildSiteMap();
}
private void CreateSiteMapRoot()
{
_siteMapRoot = new SiteMapNode(this, "Root", "~/Default.aspx", "Root");
AddNode(_siteMapRoot);
}
private void CreateSiteMapNodes()
{
SiteMapNode node = null;
for (int i = 1; i <= 3; i++)
{
node = new SiteMapNode(this,
string.Format("Child{0}", i),
string.Format("~/WebForm{0}.aspx", i),
string.Format("Child{0}", i));
AddNode(node, _siteMapRoot);
}
}
#endregion
}
}
The site map that I generate is very simple and only shows the guidelines of how to create your own provider. You can instead use a database call, web service or any other data source to create your own functionality.
Deploying the Custom Site Map Provider
After we have our custom site map provider, we need to register it in the web.config file. We need to use the siteMap
element of system.web element. This example shows how to do so with the previous custom site map provider:
<system.web>
<siteMap defaultProvider="CustomSiteMapProvider" enabled="true">
<providers>
<clear/>
<add name="CustomSiteMapProvider" type="WebApplication1.CustomSiteMapProvider"/>
</providers>
</siteMap>
</system.web>
As you can see, first I clear the default provider and then I register my own.
Using the Custom Site Map Provider
I created the following web application structure in order to demonstrate how to use the new provider:

In the Site1.Master file, I dropped a TreeView
control and a SiteMapDataSource
. All the other web forms use the master file and provide no functionality.
The code of the master page file:
<%@ Master Language="C#" AutoEventWireup="true"
CodeBehind="Site1.master.cs" Inherits="WebApplication1.Site1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView ID="TreeView1" runat="server"
DataSourceID="SiteMapDataSource1" ImageSet="Simple"
NodeIndent="10">
<ParentNodeStyle Font-Bold="False"/>
<HoverNodeStyle Font-Underline="True" ForeColor="#DD5555" />
<SelectedNodeStyle Font-Underline="True"
ForeColor="#DD5555" HorizontalPadding="0px"
VerticalPadding="0px" />
<NodeStyle Font-Names="Verdana" Font-Size="8pt"
ForeColor="Black" HorizontalPadding="0px"
NodeSpacing="0px" VerticalPadding="0px" />
</asp:TreeView>
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
The result of using this application is as follows:

Summary
Let's sum up. In the article, I demonstrated how to create your own site map provider. This is very helpful when we don’t want to use the default provider which uses a Web.sitemap XML file. After we create our provider, we need to register it in the site’s configuration file and then we can use it inside our web application.