Implementing Multiple Sitemaps

Introduction

We have a requirement in our current application to have a dynamic navigation system where certain links are shown or hidden depending on where you are within the application itself and also based on your assigned role.\ ASP.NET 2.0 provides some nice controls to handle this right out of the box.

We chose to use the TreeView control to render our navigation system. The treeview uses the SiteMapDataSource as its data source. Trimming the huge navigation tree by roles was pretty easy as it consisted of one attribute in the default sitemap provider element in the web. config is set to true.

securityTrimmingEnabled="true"

and then define which role you wanted to allow to each branch or node in the actual web. sitemap file itself.

<siteMapNode url="my/webpage.aspx" title="MyWebPage" roles="*">

That was about all we needed to get the Roles working. Getting multiple sitemaps up and running turned out to not be that much more difficult.

To enable your site to contain more than one sitemap, all you need to do is add new providers to the siteMap element in your web. config.

Example. out of the box, your web. config is configured to use a single provider like this.

<siteMap defaultProvider="DefaultSiteMapProvider" enabled="true">
    <providers>
        <add name="DefaultSiteMapProvider" description="Default SiteMap provider" type="System.Web.XmlSiteMapProvider" sitemapFile="web.sitemap" securityTrimmingEnabled="true" />
    </providers>
</siteMap>
<!-- To add more sitemaps - add more providers -->
<siteMap defaultProvider="DefaultSiteMapProvider" enabled="true">
    <providers>
        <add name="DefaultSiteMapProvider" description="Default SiteMap provider" type="System.Web.XmlSiteMapProvider" sitemapFile="web.sitemap" securityTrimmingEnabled="true" />
        <add name="MySiteMapProvider" description="My SiteMap provider" type="System.Web.XmlSiteMapProvider" sitemapFile="myWeb.sitemap" securityTrimmingEnabled="true" />
    </providers>
</siteMap>

Once you have your additional providers configured in the web config, all you need to do is bind your SiteMapDataSource to one of the providers. You can have many SiteMapDataSources on a single page as well, such as on a master page. Then, changing the sitemap being used by the current page or user role is done programmatically by assigning the DataSourceID of the navigation control to the ID of the SiteMapDataSource that you want.

switch (menuSelection)
{
    case 1:
        {
            TreeView1.DataSourceID = SiteMapDataSource1.ID;
            break;
        }
    case 2:
        {
            TreeView1.DataSourceID = SiteMapDataSource2.ID;
            break;
        }
    case 3:
        {
            TreeView1.DataSourceID = SiteMapDataSource3.ID;
            break;
        }
    default:
        {
            TreeView1.DataSourceID = SiteMapDataSource1.ID;
            break;
        }
}
TreeView1.DataBind();


Similar Articles