Click here to Skip to main content
15,867,568 members
Articles / Web Development / ASP.NET

Building a Custom Site Map Provider

Rate me:
Please Sign up or sign in to vote.
4.86/5 (15 votes)
7 Aug 2013CPOL2 min read 79.9K   28   16
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:

C#
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:

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

HTML
<%@ 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)


Written By
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: http://www.gilfink.net

Comments and Discussions

 
GeneralInteresting Pin
Yves26-Jul-10 16:43
Yves26-Jul-10 16: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.