Changing the SiteMapDataSource’s XML filename at runtime

Changing the SiteMapDataSource’s XML filename at runtime

Rate This
  • Comments 15

 

Applies to: ASP.NET 2.0

 

ASP.NET 2.0 comes with set of very rich navigation controls like TreeView, Menu, SiteMapPath. Ideally these controls should load the values at runtime from any structured data sources, be it SQL or XML or TXT. The default and most commonly used provider is XmlSiteMapProvider which accepts the data from XML file. But if you have to load different XML files at runtime, then … Here we go

 

Create two web.sitemap files

 

File 1 [web.sitemap]

=============

<?xml version="1.0" encoding="utf-8" ?>

<siteMap>

      <siteMapNode title="MyMenu 1" url="Default.aspx">          

      </siteMapNode>

</siteMap>

 

File 1 [web2.sitemap]

==============

<?xml version="1.0" encoding="utf-8" ?>

<siteMap>

  <siteMapNode title="MyMenu 2" url="Default.aspx">

  </siteMapNode>

</siteMap>

 

 

Then put any navigation control in your aspx page with the SiteMapDataSource as data source.

 

The main trick is with the web.config settings. There you have to mention all the files as the provider collection like

<?xml version="1.0"?>

<configuration>  

      <system.web>

            ………

            <siteMap defaultProvider="1SiteMap" enabled="true">

                  <providers>

                        <add name="1SiteMap" type="System.Web.XmlSiteMapProvider" siteMapFile="Web.sitemap"/>

                        <add name="2SiteMap" type="System.Web.XmlSiteMapProvider" siteMapFile="Web2.sitemap"/>

                  </providers>

            </siteMap>

      </system.web>

 

Then in the page load event of your page (where the SiteMapDataSource and navigation controls are) change the property SiteMapProvider like

     

protected void Page_Load(object sender, EventArgs e)

    {

        //The provider name mentioned in the web.config

        SiteMapDataSource1.SiteMapProvider = "2SiteMap";

    }

 

Hope this will help in simpler way, but it is recommended that you should use SqlSiteMapProvider for all dynamic scenarios.

 

Ref: http://weblogs.asp.net/scottgu/archive/2005/11/20/431019.aspx

 

Namoskar

 

Leave a Comment
  • Please add 1 and 7 and type the answer here:
  • Post
  • I tried exactly what you have said even before reading this blog. But the menu doesn't pick up the new sitemap file.

    If you have tried it & it works, do send me the code for the Page_Load.

    Andy

  • Yes I have tried and it works. The Page_Load code is  

    protected void Page_Load(object sender, EventArgs e)

       {

           //The provider name mentioned in the web.config

           SiteMapDataSource1.SiteMapProvider = "2SiteMap";

       }

    Wriju

  • Great tip. I've seen some complicated XmlDataSource methods for accomplishing this, but your way is much simpler and cleaner. Thanks.

  • Thanks for the feedback.

    Wriju

  • Hello! Very interesting. Thank you.

  • Hi Max, Thanks for the feedback.

    Wriju

  • thks for the tip...

    but in my case i was using masterpages and only work in the on_init method, so here's the code..

    protected override void OnInit(EventArgs e)

           {

           SiteMapDataSource1.SiteMapProvider = "adminSiteMap";

           base.OnInit(e);

           }

    the rest is the same.. anda i'm using vs2008 beta 2 (too much work too uninstalling :) )

  • Thanks for the tip. For VS 2008 Beta 2 uninstallation please follow,

    http://weblogs.asp.net/scottgu/archive/2007/11/20/steps-to-uninstall-vs-2008-beta2-before-installing-the-vs-2008-final-release.aspx

  • I figured this solution would work, but I don't consider it ideal for my particular issue. I'm building a site that'll use the XmlSiteMapProvider as the default provider and load Web.sitemap. Inside Web.sitemap, one of the nodes references a SQL provider I wrote to pull departmental information from the database. I want the departmental nodes to load .sitemap files, but I haven't found a way to do it, other than adding a SiteMapProvider for each individual department (in web.config). Is that doable without writing my own version of XmlSiteMapProvider or adding tons of providers in web.config?

  • Nevermind... I figured it out.

  • Ah - just what I've been loooking for!

  • Thank you chose me great passion!

    Cheers

  • Very good.. Working great. Thanks Wriju... :)

  • Great solution. Thank you.

  • It is great. It works. Thank you so much!

Page 1 of 1 (15 items)