[This article has been replaced with a new improved version.]
As outlined in the article I posted yesterday ASP.NET 2.0 provides new concepts to implement site navigation based on new server controls and the Site Map Provider concept. The controls and the provider work together and allow the implementation of a very flexible and scalable solution for site navigation.
The SiteMapProvider represents the data layer while the controls represent the presentation layer of the navigation. The SiteMapProvider provides information about the different elements in the navigation structure through SiteMapNode objects which need to be populated by the provider with the relevant information from the underlaying datasource. SiteMapNode allows to provide a Title, a URL and a Description. In addition during creation of the SiteMapNode a unique key for the Node has to be provided.
For MCMS the datasource is usually the channel structure. A SiteMapProvider for MCMS would have to read the information about the requested channel item from the MCMS repository and has to return a SiteMapNode object that contains the relevant information about this channel item:
ChannelItem ci = ...; SiteMapNode smn = new SiteMapNode(this, ci.Guid); smn.Url = ci.Url; smn.Title = ci.DisplayName; smn.Description = ci.Description;
The code above shows how to create a SiteMapNode based on a MCMS channel item. Here we are using the GUID of the channel item as unique key, the Url property for the Navigation URL. In addition we copy the Description and DisplayName properties to the Description and Title properties of the SiteMapNode object. Instead of the GUID we could also have used the Path property but as this property will later be used to retrieve the associated channel item from the repository it's better to use the GUID as a Searches.GetByGuid method call is quicker than a Searches.GetByPath method call.
A custom SiteMapProvider has to implement at least the following methods:
A SiteMapProvider can implement some more methods but only the methods above are really required for the controls shipped with ASP.NET 2.0.
A very basic SiteMapProvider for MCMS which can be used for MCMS navigation controls would look like the following:
To use the above SiteMapProvider you need to add the code above to a C# class library project and compile it into a DLL. Then add the provider to your ASP.NET 2.0 template project web.config file as follows:
<system.web> <siteMap defaultProvider="MCMSSiteMapProvider" enabled="true"> <providers> <add name="MCMSSiteMapProvider" type="StefanG.SiteMapProviders.MCMSSiteMapProvider, MCMSSiteMapProvider"/> </providers> </siteMap> </system.web>
That's all! Now the ASP.NET 2.0 navigation controls can use this SiteMapProvider. No further coding is required! It is possible to add multiple different SiteMapProviders to your site. This makes sense if your have different kind of controls where some items should be shown or hidden based on your business needs. E.g. one provider should only return channels with a specific custom property. To achieve this implement a second provider that checks for these properties in the GetChildNode method and bind this SiteMapProvider explicitly to your control.
ASP.NET 2.0 ships with three new controls that can be used for site navigation:
The SiteMapPath control - which behaves like the Woodgrove Breadcrumb control - requires a SiteMapProvider and cannot be used without it. Just drag a SiteMapPath control on your template or channel rendering script and your MCMS bread crumb is ready - if you configured the SiteMapProvider above in your web.config.
To use the Menu and the TreeView control with our SiteMapProvider you first need to drag a SiteMapDataSource object to your template or channel rendering script. Either explicitly configure the SiteMapProvider to be used using the SiteMapProvider property or let this property blank and the configured default provider will be used. Then drop the TreeView or Menu control to your template or channel rendering script and configure the SiteMapDataSource you dropped earlier as the datasource to use for the control.
One hint for the TreeView control: you should ensure that child nodes are populated on demand - otherwise all nodes are retrieved when the page is first requested which can slow down your MCMS site significantly if your provider enumerates the whole repository. To do this you need to set the PopulateOnDemand property in the TreeNode Databinding properties to true.