Page view counter

SiteMapResolve event, global.asax and defaultProvider

Last post 10-24-2007 11:34 AM by msalamon. 3 replies.

Sort Posts:

  • SiteMapResolve event, global.asax and defaultProvider

    10-23-2007, 12:36 PM
    • Loading...
    • msalamon
    • Joined on 10-23-2007, 12:31 PM
    • Posts 3
    • Points 0

    I am adding some custom behavior to the display of breadcrumbs. I had read many articles about adding an event handler in the global.axax Application_Start method to do this, as follows:

      SiteMap.Provider.SiteMapResolve += new SiteMapResolveEventHandler(CustomSiteMapHandler.Handle);

     (I have separately created the CustomSiteMapHandler class, but I could have done this within global.asax.)

    I have gotten this to work perfectly, provided that the asp:SiteMapPath tag I am using to render the breadcrumbs references the default sitemap provider.  If it references any other provider listed in web.config, however, then the event handling code is skipped entirely.

     Can anyone confirm that the event handling only works with the default sitemap provider.  If this is not the case, can anyone show me how to have the event handling code work with a non-default provider.

  • Re: SiteMapResolve event, global.asax and defaultProvider

    10-23-2007, 2:22 PM
    • Loading...
    • RichardD
    • Joined on 09-03-2002, 11:43 AM
    • Sussex, UK
    • Posts 223
    • Points 1,614
    You need to add the event handler to all of the providers, not just the default provider: 
    foreach (SiteMapProvider provider in SiteMap.Providers)
    {
        provider.SiteMapResolve += CustomSiteMapHandler.Handle;
    }
     
  • Re: SiteMapResolve event, global.asax and defaultProvider

    10-23-2007, 5:22 PM
    Answer
    • Loading...
    • msalamon
    • Joined on 10-23-2007, 12:31 PM
    • Posts 3
    • Points 0

    Thanks.  I now understand what I was doing wrong.  However, I am still having some problems, likely in part b/c this is part of sharepoint.  My web.config looked like this, with the custom provider set as the default:

         <siteMap defaultProvider="CustomProvider" enabled="true"> <!-- CurrentNavSiteMapProvider   -->
          <providers>
            <add name="CustomProvider" siteMapFile="_app_bin/xyz.sitemap" type="System.Web.XmlSiteMapProvider, System.Web, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
            <add name="CurrentNavSiteMapProvider" description="CMS provider for Current navigation" type="Microsoft.SharePoint.Publishing.Navigation.PortalSiteMapProvider, Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" NavigationType="Current" EncodeOutput="true" />
         </providers>
         </siteMap>

     I changed my code in global.asax to this:

    SiteMapProvider customSiteMapProvider = SiteMap.Providers["CustomProvider"];
    customSiteMapProvider.SiteMapResolve += new SiteMapResolveEventHandler(Handle);

    It all works fine.  but if I change my default provider to CurrentNavSiteMapProvider, then my global.asax throws this exception in the Handle() code:

      System.InvalidCastException: Unable to cast object of type 'System.Web.SiteMapNode' to type 'Microsoft.SharePoint.Publishing.Navigation.PortalSiteMapNode'.

    I put some logging code inside my Handle method:

    SiteMapNode currentNode = SiteMap.CurrentNode; 
    HttpContext.Current.Trace.Write("SiteMapProvider = " + currentNode.Provider.Name);

    The site map provider that is always listed is the default sitemapprovider, regardless of which sitemapprovider i attached the event handler to.

    Just added some more logging and found out something interesting.  currentNode.Provider.Name yields the default provider, but e.Provider (from SiteMapResolveEventArgs e) yields the custom provider.  Not sure what this means.... 

    Thus, it appears as if the event handling code is still attached to the default (sharepoint) provider.  Any ideas?

     Thanks

  • Re: SiteMapResolve event, global.asax and defaultProvider

    10-24-2007, 11:34 AM
    • Loading...
    • msalamon
    • Joined on 10-23-2007, 12:31 PM
    • Posts 3
    • Points 0

    Oops.  I have answered my own question.  In the Handle() method I was getting the currentNode from the SiteMap object (the default one), not from the custom provider.  once I changed that, it worked fine.

Page 1 of 1 (4 items)