Click here to Skip to main content
Licence CPOL
First Posted 24 Jul 2010
Views 17,699
Bookmarked 17 times

Building a Custom Site Map Provider

By | 31 Jul 2010 | Article
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 verride 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:

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" 
	"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:

Generated TreeView

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.

License

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

About the Author

Gil Fink

Architect
Sela Group
Israel Israel

Member

Gil Fink is an expert in ASP.NET and Microsoft data platform and serves as a Senior Architect at SELA Group. He is a Microsoft data platform MVP and a certified MCPD Enterprise Application Developer. Gil has worked in the past in variety of positions and projects as a leading developer, team leader, consultant and more. His interests include Entity Framework, Enterprise Library, WCF, LINQ, ADO.NET and many other new technologies from Microsoft.
 

My technical blog: http://www.gilfink.net

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberPrince Ali Khan16:15 29 Jul '10  
GeneralGood one ! Pinmemberkevin haesendonckx23:17 26 Jul '10  
GeneralInteresting PinmemberYves16:43 26 Jul '10  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120528.1 | Last Updated 31 Jul 2010
Article Copyright 2010 by Gil Fink
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid