New ASP.NET 2.0 SQL Site Map Provider

Jeff Prosise has a written a great sample and article for the February edition of MSDN magazine that walksthrough the new SQL Server based provider he built for the ASP.NET 2.0 Site Navigation System.  This enables you to store your site map structure in a database instead of using the default XML Provider that comes default.  Enabling it within an ASP.NET 2.0 site is as simple as configuring the provider in your web.config file -- no code needs to change in order to take advantage of it (and you can databind any page control to it using the new SiteMapDataSource control).

What is really cool about his provider is that it also uses the new SQL Cache Invalidation feature to cache the sitemap structure in-memory within ASP.NET (avoiding the need to hit the database each request to retrieve the data), but automatically regenerate it anytime the site-map structure changes in the database (ensuring that your site structure is always up to date).

You can read all about it and download the code from here.

Here are a few of my past posts on the new ASP.NET 2.0 Site Navigation system if you are interested in learning about it more:

Hope this helps,

Scott

 

20 Comments

  • Scott,



    I apologize again if my question creeps out of the scope of your article:



    I have seen plenty of examples of creating site map nodes from a database, however in my case I am also storing localization data.



    So, I have a site map provider that is loading the nodes from my database, but now I need to load the language specific titles and descriptions from my database as well.



    It seems the most flexible overload for the SiteMapNode constructor allows for passing an explicit name-value-pairing of resource idenfifiers, or a single implicit key:



    new SiteMapNode(SiteMapProvider provider, string key, string url, string title, string description, IList roles, NameValueCollection attributes, NameValueCollection explicitResourceKeys, string implicitResourceKey);



    I would be happy to use either parameter, however I have not had any luck finding examples on how to generate string resources dynamically.



    If you could point me in the right direction (or tell me if I'm trying to do something impossible) it would be a big help!



    Thanks again,

    Roger

  • One thing I'm noticing a lot of users ask for at the ASP.NET forums is an updatable SiteMapProvider. The invalidation features of ASP.NET can also be applied file changes. In this context, the same strategy Jeff uses to invalidate and refresh his SiteMap can be applied to file changes as well.



    In short, the example from Jeff can also be used as a great model for creating updatable SiteMapProviders. I hope a lot of users can take advantage of this to improve their own custom SiteMapProviders from caching to dynamically updating. This doesn't need to only be applied to SQL.

    --

    Danny

  • I am struggling to find a way to declare a siteMap that has a dynamic location for the sitemap xml file. Can I declare the siteMapFile at runtime or dynamically add a provider somehow? Any help is greatly appreciated.

    Thanks
    Steve

  • Steve: the site map provider can be changed anytime on the SiteMapDataSource. Does that help?

  • I am trying to make som SiteMapNodes - and to make use of explicit localization.
    Following your exelent advice - I have one problem.
    How do you construct a NameValueCollection that contains an string[] as value?
    The NameValueCollection.Add() only accepts 2 stings.

    Regards Anders Djursaa

  • Hi Anders,

    I don't see an overloaded method on NameValueCollection that accepts a string. What I think you'll have to-do is to just call the Add method multiple times.

    Hope this helps,

    Scott

  • Hi Ken,

    Can you send me email directly with this question? I will then loop in a few folks who know more about this to help.

    Thanks,

    Scott

  • Hi

    I ran into a problem using the SqlSiteMapProvider. The StaticSelectedStyle is unfortunately not applied as expected.
    What is required that this style is applied?

    Thanks in advance for your help.

    Regards,
    eru

  • I am still having problems with the SQL Site Map Provider. It only calls the sproc (in BuildSiteMap()) only once. I can logout or even re-start the web app. and it will still use old data. I have disabled SQL Caching on the database by running aspnet_regsql -dd and I have also removed the attribute sqlCacheDependency from the web config. What else do I need to do to force the SqlSiteMapProvider class to initialize every time?

  • Hi Marcelo,

    If you can send me an email, I will loop you in with someone on my team who might be able to help.

    Thanks,

    Scott

  • Hi JDS,

    You might be able to access Session information by writing code like this:

    HttpContext.Current.Session("somevalue")

    I'm not 100% sure where/when the above method gets called - so am not sure if the session data is always there or not. But this might get you started down the right path.

    Hope this helps,

    Scott

  • Hi Scott,

    I am having the same problems as Marcelo. No matter what I do I can not reload the site map. Did you find out any information from your team that could help with this situation?

    Thanks,

    Todd

  • how would it be possible to Filter the nodes if i use this custom Site Provier.for eg if i have 2 master pages but the contents need to be varied .
    thnx
    don

  • Hi Donrajah,

    You should be able to use the standard SiteMap mechanisms for varying the content regardless of which SiteMap Provider you use.

    Hope this helps,

    Scott

  • Hi,
    I have this working on my website, but now I need to add another menu for the administration section. What is the best approach to achieve this?

    Should I try and create another provider and use 2 sp's e.g. GetSiteMap and GetAdminSiteMap or just use one sp and provider and try to filter the items in the buildsitemap method?

    Thanks in advance.

  • Hi Greg,

    You can actually register multiple SiteMap providers in your web.config file - and then point the SiteMapDataSource control at the provider you wish to use. So that would be one option to use.

    Hope this helps,

    Scott

  • Hi Scott,

    I am having the same problems as Marcelo and Todd. I can't reload the site map. Can i force a reload ie. at login? Any ideas?

    Thanks,
    smeja

  • Hi Smeja,

    Do you have the SQL Cache Invalidation feature enabled with it? I believe this will automatically invalidate the cache when the database contents change.

    Thanks,

    Scott

  • Hi Scott,
    I have multiple sitemapproviders and the sitemapdatasource is placed on the masterpage. I set the sitemapprovider depending on the state of the user that logs in. This works perfectly, however when I change pages (all my pages are content pages) the sitemapprovider defaults to the original. Do you know why?

  • Hi Romie,

    The master page runs on each request - so by default it will revert back to its default setting when the user navigates to a new page.

    You'll want to set this value on each page request to the application.

    Thanks,

    Scott

Comments have been disabled for this content.