Aidan Garnish

Collaboration Not Competition

Custom SiteMapPath to handle variations in MOSS 2007

Whilst developing a MOSS 2007 Internet site I came across an issue with the SiteMapPath control when using variations. Variations use the root site to direct the user to the correct variation for their settings. A user with settings of en-GB, for example, will be directed towards the relevant variation by variationroot.aspx.

This is great except when using the SiteMapPath breadcrumb navigation control which displays the variation root site as well as the relevant variation site by default.

By default the following is displayed:

Variation Root > English Site > Some Page

What I want to display is:

English Site > Some Page

To achieve this I have created a server control that inherits from SiteMapPath and has an additional property called IgnoreNode. I have overridden the RenderContents event so that the HyperLink that has the same text as IgnoreNode is no longer rendered. In my case I set IgnoreNode to "Variation Root" so that it is not displayed in the breadcumb but you could choose any node that you don't want to display.

The code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CustomBreadcrumb
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:CustomBreadcrumb runat=server></{0}:CustomBreadcrumb>")]
    public class CustomBreadcrumb : SiteMapPath
    {
        [Bindable(true),
        Category("Data"),
        DefaultValue("")]
        public string IgnoreNode
        {
            get
            {
                object o = ViewState["IgnoreNode"];
                if (o != null)
                    return (string)o;
                else
                    return String.Empty;
            }

            set
            {
                ViewState["IgnoreNode"] = value;
            }
        }
       
       protected override void RenderContents(HtmlTextWriter output)
       {
          bool blnNodeWasIgnored = false;
          HyperLink hlLink;

          foreach (Control oControl in this.Controls)
          {
              SiteMapNodeItem oSiteMapNodeItem = (SiteMapNodeItem)oControl;

              foreach (Control aControl in oSiteMapNodeItem.Controls)
              {
                  if(aControl.GetType().ToString() == "System.Web.UI.WebControls.HyperLink")
                  {
                      hlLink = (HyperLink)aControl;

                      if (hlLink.Text == IgnoreNode)
                      {
                         //don't call render
                          blnNodeWasIgnored = true;
                      }
                      else
                      {
                          aControl.RenderControl(output);
                          blnNodeWasIgnored = false;
                      }
                  }

                  if (aControl.GetType().ToString() == "System.Web.UI.WebControls.Literal")
                  {
                      if (blnNodeWasIgnored == true)
                      {
                          //don't render the seperator
                      }
                      else
                      {
                          aControl.RenderControl(output);
                      }
                  }
              }                  
          }
       }
    }
}

To deploy this control it needs to be strongly named and installed to the GAC using gacutil.exe

An entry in the web.config of your web application needs to be added:

<SafeControl Assembly="CustomBreadcrumb, Version=1.0.0.0, Culture=neutral, PublicKeyToken=91e52569228df0da" Namespace="CustomBreadcrumb" TypeName="*" Safe="True" />

(Use reflector to find out the public key token of your dll.)

Restart IIS or even better recycle the relevant application pool.

You will need to register the control by adding the following line in the master page:

<%@ Register TagPrefix="adg" Namespace="CustomBreadcrumb" Assembly="CustomBreadcrumb, Version=1.0.0.0, Culture=neutral, PublicKeyToken=91e52569228df0da" %>

Finally add the control to your master page, publish and approve the page.

Add comment

Loading