WSSv3 Using a custom topnavbar using XMLSitemap

After provisioning the site as seen in: How to provision a site (with master page and default.aspx) in WSSv3 using a site definition I want to add a custom menu to the topnavbar. I decided to use the XmlSiteMapProvider so I can use a simple XML file to define the menu layout. When you want to use a custom XML you have to add a value to the web.config of the sharepoint site to point to the XML file, i want this menu to be added by default when a site is created so i will add it to my site provision

Step 1: Create a sitemap file and place it in TEMPLATELayoutsCmenu.Sitemap, for example:

Step 2: Add the xml file as a new sitemapprovider to the web.config in a feature:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
  {     SPWeb siteCollection = (SPWeb)properties.Feature.Parent;
        SPWebApplication webApp = siteCollection.Site.WebApplication;

        SPWebConfigModification modification = new SPWebConfigModification();
        modification.Path = “configuration/system.web/siteMap/providers”;
        modification.Name = “add[@name=’CMenu’]”;
        modification.Sequence = 0;
        modification.Owner = “FeatureReceiverSiteLayout”;
        modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
        modification.Value = ““;

     //    webApp.WebConfigModifications.Remove(modification);
        // Save web.config changes.
        // Serialize the web application state and propagate changes across the farm.
Use the webApp.WebConfigModifications.Remove part to remove the item from the web.config. Take note the FeatureDeactivating will not be be called when a site is deleted, use the FeatureUninstalling when you want the the item removed from the web.config at site delete.

Step3: Change your master page with the following:

  • find the asp:ContentPlaceHolder id=”PlaceHolderHorizontalNav” tag, place the following above it: ““. This creates the link from SiteMapDataSource to your XmlSiteMapProvider.
  • Edit the datasource propertie of  asp:ContentPlaceHolder id=”PlaceHolderHorizontalNav” to: DataSourceID=”SMCMenu”

The page will now use the your XML file as navigation

Step 4: When provisioning a site using a site definition you would like the feature to autmomaticly add the changes to the web.config. To do this you have to add:

  • Find the ID of the feature in feature.xml
  • Find the node in ONET.XML
  • Place a new WebFeatures in de the WebFeatures tag under configuration “
  • Now the feature will be activtivated on site creation.

Keep in mind that the feature has to be deployed to the server first, again this is posible with WSPBuilder