Print Shortlink

How to: Auto-provisioning SharePoint 2010 navigation

When you have to provision a SharePoint site with a pre-defined template, one of the most common items you will be asked to add is navigation. After all, your users want to see their quick launch and global navigation, way before the rest of the “lorem ipsum” pages are in place since those are core elements of the site user interface.

Since provisioning quick launch and global navigation menu items is less than straightforward, I’m going to give a quick hot-to of how to do that in this post. In here, I`m assuming you`re using SharePoint 2010 publishing site template, so we`re all on the same page.

Quick launch, usually located on the left hand side of the page

.. and to manage the quick launch menu using a UI you would navigate to Site Actions -> Site Settings -> Navigation

To manage quick launch programatically, you would execute a code in a feature receiver or a console app.

Here is how you get a hold of the quick launch and clear all of its existing nodes (Lists, Webs etc):

SPSite site = properties.Feature.Parent as SPSite;
SPWeb web = site.RootWeb; PublishingWeb = PublishingWeb.GetPublishingWeb(web); SPNavigationNodeCollection nodes publishingWeb.Navigation.CurrentNavigationNodes;

// clearing old quicklaunch navigation nodes

while (nodes.Count>0)
{
 nodes.Delete(nodes[0]);
}

Once cleared, here is how you add new node of type Heading

// setting quick launch navigation nodes

SPNavigationNode navNode1 = SPNavigationSiteMapNode.CreateSPNavigationNode
("Navigation Node Title", "/", NodeTypes.Heading, nodes);

.. and once you’ve got the heading node, you may want to add sub-nodes aka “links” to it, like this:

SPNavigationNode subNode = SPNavigationSiteMapNode.CreateSPNavigationNode
("Sub Node Title", "/", NodeTypes.AuthoredLinkPlain, navNode1.Children);
subNode.Update();

The rules with global navigation are exactly the same except here you get a hold of the :

publishingWeb.Navigation.GlobalNavigationNodes

Enjoy!