Click here to Skip to main content
14,427,915 members

Building a Custom Site Map Provider

Rate this:
4.86 (15 votes)
Please Sign up or sign in to vote.
4.86 (15 votes)
7 Aug 2013CPOL
The article explains how to build a simple custom site map provider.


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;


        #region Methods

       public override SiteMapNode BuildSiteMap()
            // Use a lock to provide thread safety
            lock (_siteMapLock)
                if (_siteMapRoot != null)
                    return _siteMapRoot;



                return _siteMapRoot;

        protected override SiteMapNode GetRootNodeCore()
            return BuildSiteMap();

        private void CreateSiteMapRoot()
            _siteMapRoot = new SiteMapNode(this, "Root", "~/Default.aspx", "Root");

        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);


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:

  <siteMap defaultProvider="CustomSiteMapProvider" enabled="true">
      <add name="CustomSiteMapProvider" type="WebApplication1.CustomSiteMapProvider"/>

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:

Sample Site

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" 

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:TreeView ID="TreeView1" runat="server" 

		DataSourceID="SiteMapDataSource1" ImageSet="Simple"

            <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:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">

The result of using this application is as follows:

Generated TreeView


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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Gil Fink
Technical Lead sparXys
Israel Israel
Gil Fink is a web development expert and ASP.Net/IIS Microsoft MVP. He is the founder and owner of sparXys. He is currently consulting for various enterprises and companies, where he helps to develop Web and RIA-based solutions. He conducts lectures and workshops for individuals and enterprises who want to specialize in infrastructure and web development. He is also co-author of several Microsoft Official Courses (MOCs) and training kits, co-author of "Pro Single Page Application Development" book (Apress) and the founder of Front-End.IL Meetup. You can read his publications at his website:

Comments and Discussions

Member 1171673627-Oct-15 4:45
MemberMember 1171673627-Oct-15 4:45 
QuestionAbout Multiple Parent Node Pin
Member 1137389913-Jan-15 22:04
MemberMember 1137389913-Jan-15 22:04 
AnswerRe: About Multiple Parent Node Pin
Gil Fink17-Jan-15 23:37
MemberGil Fink17-Jan-15 23:37 
BugRelative URLs Pin
freezer2722-Dec-14 13:24
Memberfreezer2722-Dec-14 13:24 
GeneralRe: Relative URLs Pin
Gil Fink17-Jan-15 23:41
MemberGil Fink17-Jan-15 23:41 
QuestionMy Vote is 5 Pin
Stephen Wu 713-Oct-13 8:39
MemberStephen Wu 713-Oct-13 8:39 
AnswerRe: My Vote is 5 Pin
Gil Fink13-Oct-13 21:12
MemberGil Fink13-Oct-13 21:12 
GeneralMy vote of 5 Pin
Joezer BH7-Aug-13 2:51
professionalJoezer BH7-Aug-13 2:51 
GeneralRe: My vote of 5 Pin
Gil Fink7-Aug-13 19:22
MemberGil Fink7-Aug-13 19:22 
Questionexcellent Pin
peter gabris6-Aug-13 10:00
Memberpeter gabris6-Aug-13 10:00 
AnswerRe: excellent Pin
Gil Fink7-Aug-13 1:03
MemberGil Fink7-Aug-13 1:03 
QuestionThank you Pin
Mahadevi Kumbar12-Jun-13 21:30
MemberMahadevi Kumbar12-Jun-13 21:30 
AnswerRe: Thank you Pin
Gil Fink12-Jun-13 22:03
MemberGil Fink12-Jun-13 22:03 
GeneralMy vote of 5 Pin
Prince Ali Khan29-Jul-10 17:15
MemberPrince Ali Khan29-Jul-10 17:15 
GeneralGood one ! Pin
kevin haesendonckx27-Jul-10 0:17
Memberkevin haesendonckx27-Jul-10 0:17 
GeneralInteresting Pin
Yves26-Jul-10 17:43
MemberYves26-Jul-10 17:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 24 Jul 2010

Tagged as


27 bookmarked