ASP.NET 2.0 Changing SiteMap files
I have been testing out navgation in ASP.NET 2.0, especially the sitemap feature.I wanted to dynamically change sitemaps and found that it can be done as follows:
First define a few different sitemap files (i.e. x.sitemap, y.sitemap) within your project (i.e. Company1SiteMap, Company2SiteMap).
Then in the web.config file add the following entries:
<siteMap defaultProvider="Company1SiteMap" enabled="true">
<providers>
<add
name="Company1SiteMap"
type="System.Web.XmlSiteMapProvider"
siteMapFile="~/x.sitemap" />
<add
name="Company2SiteMap"
type="System.Web.XmlSiteMapProvider"
siteMapFile="~/y.sitemap" />
</providers>
</siteMap>
If a siteMapPath control is used on a form without specifing a siteMapProvider then the default provider is used which by default is a project file named web.sitemap. The default is changed (in this case to x.sitemap) by the web.confg entry:
<siteMap defaultProvider="Company1SiteMap" enabled="true">
The sitemap file defined by the Company1SiteMap <providers> entry is now the default sitemap for the project.
To define other sitemaps for the project, add entries in the <providers> section as shown in the above web.config entries.
To dynamically change the sitemap used by a sitemappath control just set the SiteMapProvider propery of the control with the name of the entry that points to the .sitemap file that is desired:
(Assume a sitemap control on the form named SiteMapPath1)Me.SiteMapPath1.SiteMapProvider = "Company2SiteMap" 'will use y.sitemap
Now, the SiteMapPath1 sitemappath control will use the sitemap file defined by the entry named Company2SiteMap (y.sitemap)
I tested this by changing the sitemap from the default (x.sitemap) to y.sitemap in a button click event and it worked like a champ.
1 Comments:
Good post; I figured this is how it would be done. My situation is a little different; I'm trying to control the provider for a sitemap that's nested inside of a .ascx file. Any idea on the correct pathing to control this provider?
Post a Comment
<< Home