This article replaces my earlier article about MCMS Navigation Providers which I have written while ASP.NET 2.0 was in Beta 2. After Beta 2 a couple of things have changed which prevent the earlier code from working properly with the final version of ASP.NET 2.0.
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 unique Key, 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); // we use the GUID as the Key for the Node.
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 for MCMS should implement at least the following methods:
- public override SiteMapNode FindSiteMapNode(string GuidOrUrl)
This method returns a SiteMapNode that is identified by a specific posting or channel Guid or an Url.
Note: this method was different in my earlier version of the SiteMapProvider as the behavior was different in ASP.NET 2.0 Beta 2. In the beta the input parameter was always an URL. Now the Key value of the SiteMapNode – which represents the GUID of the MCMS object – or an Url can be is passed in.
- public override SiteMapNode FindSiteMapNode(HttpContext context)
This method returns a SiteMapNode for the node in the provided HttpContext.
Note: this method is new in my SiteMapProvider implementation compared with the version in my previous article.
- public override SiteMapNodeCollection GetChildNodes(SiteMapNode node)
This method returns a collection of SiteMapNodes for all child objects of a given node.
- public override SiteMapNode GetParentNode(SiteMapNode node)
This method returns the parent node of a given node.
- protected override SiteMapNode GetRootNodeCore()
This method returns the root node of the Site Tree.
A SiteMapProvider can implement some more methods but only the methods above are really required for the navigation controls shipped with ASP.NET 2.0 to work correct with MCMS 2002.
A very basic SiteMapProvider for MCMS which can be used by the ASP.NET 2.0 navigation controls would look like the following:
public class MCMSSiteMapProvider : SiteMapProvider
// Here we copy the relevant information from the channel item into the SiteMapNode
// object. We are using the Display Name as the Title and the GUID of the channel item
// as the unique key for the SiteMapNode object
// This allows easy lookup of the channel item in the MCMS repository.
protected SiteMapNode GetSiteMapNodeFromChannelItem(ChannelItem ci)
SiteMapNode smn = null;
if (ci != null)
smn = new SiteMapNode(this, ci.Guid);
smn.Url = ci.Url;
smn.Title = ci.DisplayName;
smn.Description = ci.Description;
// Return a SiteMapNode for a given MCMS channel item identified by GUID or Url
public override SiteMapNode FindSiteMapNode(string GuidOrUrl)
ChannelItem ci = null;
ci = CmsHttpContext.Current.Searches.GetByGuid(GuidOrUrl) as ChannelItem;
ci = EnhancedGetByUrl(CmsHttpContext.Current, GuidOrUrl);
// Return a SiteMapNode for the given HttpContext.
// As this context is always the current context it is fine to return the current MCMS channel item.
public override SiteMapNode FindSiteMapNode(HttpContext context)
ChannelItem ci = CmsHttpContext.Current.ChannelItem;
// Return a collection of SiteMapNodes for all child elements of the current channel.
// Here both Channels and Postings are returned. This method can be tailored to your needs.
public override SiteMapNodeCollection GetChildNodes(SiteMapNode node)
SiteMapNodeCollection smnc = new SiteMapNodeCollection();
Channel channel = CmsHttpContext.Current.Searches.GetByGuid(node.Key) as Channel;
if (channel != null)
ChannelCollection cc = channel.Channels;
foreach (Channel c in cc)
PostingCollection pc = channel.Postings;
foreach (Posting p in pc)
// Return the SiteMapNode for the parent object of the current Node.
// This method is called by the SiteMapPath control (the one that can be used as Breadcrumb controls)
// Here we go up to the root channel. This method can be tailored to your needs
// if you would to return only parts of the path
public override SiteMapNode GetParentNode(SiteMapNode node)
ChannelItem ci = CmsHttpContext.Current.Searches.GetByGuid(node.Key) as ChannelItem;
ChannelItem parent = null;
if (ci == null)
// Return the SiteMapNode for the root object of the tree. Here we return the root channel
// This method can be tailored to your needs if you would like to start at a different level.
protected override SiteMapNode GetRootNodeCore()
Channel root = CmsHttpContext.Current.RootChannel;
// feature is enabled or not
private bool MapChannelToHostHeaderEnabled(CmsContext ctx)
return (ctx.RootChannel.UrlModePublished == “http://Channels/”);
// Replacement for the Searches.GetByUrl method as the original one
// does not work correct with host header mapping enabled.
// details: http://support.microsoft.com/?id=887530
private ChannelItem EnhancedGetByUrl(CmsContext ctx, string Url)
string Path = HttpUtility.UrlDecode(Url);
Path = Path.Replace(“http://”, “/Channels/”);
Path = “/Channels/” +
HttpContext.Current.Request.Url.Host + Path;
Path = Path.Substring(0, Path.Length – 4);
Path = Path.Substring(0, Path.Length – 1);
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:
<siteMap defaultProvider=”MCMSSiteMapProvider” enabled=”true“>
<add name=”MCMSSiteMapProvider” type=”StefanG.SiteMapProviders.MCMSSiteMapProvider, MCMSSiteMapProvider“/>
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.
In the next article I will discuss an additional problem with the TreeView control which only occurs in the final version of ASP.NET 2.0 and not with the Beta-2 bits and which prevents the TreeView control from working correct on a MCMS template or channel rendering script.