Custom SitemapProvider with multiple sitemap files

Hi, today I ran into a problem using Multiple sitemap files in combination with a custom sitemapprovider inheriting from the XmlSiteMapProvider class. Since I couldn't find a solution to this on the web, I thought i'd share my findings ;-)

When using multiple sitemap files, you can either add a sitemapnode to your .sitemap indicating a provider for this node, or you can add a sitemapnode pointing to a different .sitemap.

  1. <siteMapNode provider="myProvider" /> or
  2. <siteMapNode siteMapFile="morenav.sitemap" />

Problem with the first solution is that you have to explicitly point to a provider in your web.config and thus add a provider for each sitemapfile. This creates an unwanted dependency, especially (as is the case in my situation) when the additional sitemapfile is created by someone else (a different project team for example). More importantly, you just can't go around adding providers for each external sitemapfile. It works though!

Problem with the second solution is that you can't get it to work with your custom Sitemap provider. Let's say we configure the default sitemapprovider to use a custom provider, say MySitemapProvider. When using multiple sitemap files, you'll find the custom provider is used for your main sitemap, but not for your referenced sitemap.

It took me a while to figure it out, but this is what you might call a bug in Asp.Net 2.0. It took a deep dive into reflector and some major frustration to figure it out, but the .Net framework explicitly uses the standard XmlSiteMapProvider whenever you apply the siteMapFile attribute on one of your nodes. Since these methods are private and use sealed system.web classes I couldn't find a way to program around this. If anybody has thoughts on this, feel free to share!

Oh, and another thing: the provider attribute always take precendence. You would think you could define a siteMapFile on your node and a provider to make it more generic, but you can't!

Hope this saves someone the trouble.

Cheers,
rinze

Published Thursday, February 07, 2008 6:48 PM by rinze

Comments

# re: Custom SitemapProvider with multiple sitemap files

Thursday, February 07, 2008 10:09 PM by Robert McLaws

The solution is to not specify the Provider information in the XML file, but in the SiteMapDataSource controls you use on the page.

# re: Custom SitemapProvider with multiple sitemap files

Friday, February 08, 2008 4:40 AM by Dave Sussman

Bug, feature, expected behaviour? it only happens in the XmlSiteMapProvider, so you could argue that it makes sense for external nodes to also come from an xml file. But, it does break with the ethos of the flexible provider pattern.

If you're writing a custom provider that inherits from the XmlSiteMapProvider then you need to override BuildSitemap so that you convert the nodes yourself, instead if it automatically assuming they'll be from the XML provider.

Personally I think you're better off creating custom providers from a lower level, StatisSiteMapProvider or SiteMapProvider; you then have more control.

# re: Custom SitemapProvider with multiple sitemap files

Friday, February 08, 2008 4:42 AM by rinze

Hi robert. I don't think this will solve it. A menu control would point to a datasource, which in turn uses a web.config configured SiteMapProvider.

For .sitemap files referenced in your main sitemap with the siteMapFile attribute, the provider configured with the datasource control will not be used

# re: Custom SitemapProvider with multiple sitemap files

Friday, February 08, 2008 4:55 AM by rinze

Hi dave. The fact that it breaks the provider pattern is exactly the problem indeed. It's unexpected and difficult to figure out.

Good point on the StaticSitemapProvider. I'm all for reuse and a lot of the work has been done in the XmlSiteMapProvider. Because of this.. anomaly...it's unfortunately useless to me. I'll make my provider inherit from the StaticProvider and implement the BuildSiteMap method. Thanx!

# re: Custom SitemapProvider with multiple sitemap files

Monday, March 10, 2008 7:06 PM by Lynette

I have been reading the entry here and I am not 100% sure if this is the same issue I am having.  I basically have 2 navigations (1 on master page and 1 in the content page)  Each navigation will be dependent on what divison or job title of the person logged in. I do not want to hard code the file in the provider declaration b/c code needs to run to see who they are and then what title they are and so on.  I want to dynamically in the page_load of the control to specify the siteMapFile of the SiteMapProvider.  I am not having any luck dynamically settign this siteMapFile at all.  As long as it is hard coded in the web.config it  is happy ... any ideas or suggestions.  Thanks...  Lynette.temple@phoenix.gov

# re: Custom SitemapProvider with multiple sitemap files

Tuesday, March 25, 2008 4:22 AM by rinze

Hi Lynette,

is it necessary to use multiple sitemapfiles? Ofcource it depends on the size, but you could consider using a single sitemap file and employ security trimming to load the appropriate branch. The job title in this case represents the role.

hope this helps!

# re: Custom SitemapProvider with multiple sitemap files

Monday, May 12, 2008 4:11 AM by Sravan Kasyap K

Hi Rinze,

I have a small requirement. I hope you would help me as you have played around with SiteMap. I added a new entry in the web.config of wss site my virtual directory as below.

 <add name="MySiteMapProvider"

description="SiteMap provider which reads in .sitemap XML file."

type="System.Web.XmlSiteMapProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

siteMapFile="Custom.sitemap" securityTrimmingEnabled="true" />

I have created my custom.sitemap and placed the same in wss virtual directory. Now i want to show nodes based on sharepoint in built roles. If a user has full control permission then he should be able to see administration page. I have put sitemap node entry in custom.sitemap as below.

<siteMapNode key="Administration" url="~/Pages/WiproAdministration.aspx" title="ADMINISTRATION" roles="Full Control">

</siteMapNode>

But all users on the site are able to see the page. How can i get this to work? Please reply to sravankasyapk@gmail.com

Leave a Comment

(required) 
(required) 
(optional)
(required)