DCSIMG
Building a Custom Site Map Provider - Gil Fink's Blog

Gil Fink's Blog

Fink about IT

News

Microsoft MVP

My Facebook Profile My Twitter Profile My Linkedin Profile

Locations of visitors to this page

Creative Commons License

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2012 Gil Fink

Hebrew Articles

Index Pages

My OSS Projects

English Articles

Building a Custom Site Map Provider

Building a Custom Site Map Provider

In the last ASP.NETBuilding a Custom Site Map Provider
course I delivered I
was asked how to
build a custom
site map provider
.
This post holds the answer.
You can download the full example from here.

Building a Custom Site Map Provider

There are times that 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. In these times we can create a
custom site map provider. What we need to do is to inherit from the
StaticSiteMapProvider class and then to 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 return the root node of the built site map.
The following code demonstrate how I 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 show the guidelines
of how to create your own provider. You can use instead 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:
Generated TreeView

Summary

Lets sum up, in the post 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.

DotNetKicks Image

Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# October 23, 2009 4:09 PM

DotNetBurner - XML said:

DotNetBurner - burning hot .net content

# October 24, 2009 12:34 AM