Implementing multiple sitemaps


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 upon 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 it's datasource. 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 being set to true,

securityTrimmingEnabled="true"
   

and then defining 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 to add new providers to the siteMap element in your web.config.  For 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"></add>

</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>           

<add name="MySiteMapProvider" description="My SiteMap provider. " type="System.Web.XmlSiteMapProvider" sitemapfile="myWeb.sitemap" securitytrimmingenabled="true"></add>

</providers>

</siteMap>

Once you have your additional providers configured in the web.config, then all you need to do is to bind your SiteMapDataSource to one of the providers. You can have many SiteMapDataSources on a single page as well, such as in a masterpage. Then, changing the sitemap being used by the current page or users role is done programatically 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();

Up Next
    Ebook Download
    View all
    Learn
    View all