Configuring Multiple Sitemaps in .NET

December 7, 2007 at 1:12 pm (.NET, ASP.NET, Web Design)

Sitemaps are used to describe the structure of the site so that the site navigation controls such as menu, treeview etc can use it to display the site structure properly. Generally the owner of the site will want to display a separate navigation for anonymous and logged users. For this the menu structure will vary. In this case we can use multiple sitemaps to display the appropriate menu based on user.

For using multiple sitemaps you need to configure the sitemap in the web.config file. We will learn this through an example. Suppose we need to site that allows the logged user to buy the product and the anonymous user is allowed only to browse the product. For this we need to display buy & browse in the menu for the logged user and browse for anonymous user. To implement this we can use multiple sitemaps.

Create a new website and add the two sitemaps for this namely logged and anonymous. In the logged.sitemap add two sitemap nodes, Browse and Buy. In the anonymous.sitemap create only one sitemap node, Browse.

Now configure the two sitemaps in the web.config file. Under System.Web tag add the following sitemap providers as shown below.

<siteMap defaultProvider =”AnonymousUser”>
<providers>
<add
name=”loggedUser”
type=”System.Web.XmlSiteMapProvider”
siteMapFile=”~/logged.sitemap” />
<add
name=”AnonymousUser”
type=”System.Web.XmlSiteMapProvider”
siteMapFile=”~/Anonymous.sitemap” />
</providers>
</siteMap>

Note:-

The defaultProvider is assigned to AnonymousUser which will load that menu by default.

Now create a menu control and an asp sitemapdatasource control.

<asp:Menu DataSourceID = “sitemapds” Orientation = “Vertical” runat = “server”>
</asp:Menu>
<asp:SiteMapDataSource ID = “sitemapds” runat = “server” ShowStartingNode = “false” />

In the code behind add the logic to find if the user is logged in or not. Here i have added a button which when clicked will make the user logged in. Now add the code that will assign the change the sitemapprovider to LoggedUser to display the menu when the user is logged.

Protected Sub loginBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles loginBtn.Click
userstatus = “logged”
If userstatus = “logged” Then
sitemapds.SiteMapProvider = “loggedUser”
Else
sitemapds.SiteMapProvider = “AnonymousUser”
End If
End Sub

This is how we configure multiple sitemaps.

8 Comments

  1. Sundar said,

    nice da…. πŸ™‚

  2. Christer said,

    Your explanation is much better then what Microsoft provides. You might have some thing to learn Microsoft.

  3. Typh said,

    thx a lot, really need this πŸ™‚

  4. Trupti said,

    thank you so much. This helped a lot.

  5. saloni said,

    Nice one … but how to perform same task without treeView or Menu control.
    only using sitemappath control

  6. meiaowsh said,

    thank you so much! this really helped me. \(^-^)/

  7. Harsha Vardhan said,

    This post helped me solving the problem, Thanks!

  8. karan110011@gmail.com said,

    Very Helpfull, tanks a lot

Leave a reply to Sundar Cancel reply