Friday, December 26, 2008

Programmatically customize site navigation

The SPWeb object has been enhanced in MOSS 2007 to support the ability to control the navigation of a SharePoint site programmatically!

The SPWeb object has a new property named Navigation that returns a SPNavigation object. This object allows developers to programmatically control the navigation of a SharePoint site.

Here is how you add a menu item to the QuickLaunch navigation menu.

These QuickLaunch examples assume you have created a top level site named quicklaunch.

Once this top level site has been created its URL will look like this: http://komal/quicklaunch
In this example we will add two links to the QuickLaunch menu for the quicklaunch top level site, one link will be internal and one will be external. The internal link will point to the Links list in the QuickLaunch site. The external link will point to the SharePoint Experts web site.

SPSite quickLaunchSite = new SPSite(“http://komal/quicklaunch”);

SPWeb quickLaunchWeb = quickLaunchSite.OpenWeb();

SPNavigationNodeCollection quickLaunchNodes = quickLaunchWeb.Navigation.QuickLaunch;

SPNavigationNode internalMenuItem = new SPNavigationNode(“Links”, “Lists/Links/AllItems.aspx”, false);

quickLaunchNodes.AddAsFirst(internalMenuItem);

SPNavigationNode externalMenuItem = new SPNavigationNode(“SharePoint Data”, “http://SharePointdata.blogspot.com”, true);

quickLaunchNodes.AddAsFirst(externalMenuItem);

quickLaunchWeb.Update();

Here is how you remove a menu item from the QuickLaunch navigation menu.

In this example we will remove the external link to the SharePoint Experts web site.

SPSite quickLaunchSite = new SPSite(“http://komal/quicklaunch”);

SPWeb quickLaunchWeb = quickLaunchSite.OpenWeb();

SPNavigationNodeCollection quickLaunchNodes = quickLaunchWeb.Navigation.QuickLaunch;

quickLaunchNodes.Delete(quickLaunchNodes([0]));

quickLaunchWeb.Update();

Here is how you add grouped menu items to the QuickLaunch navigation menu.

In this example we will create a header link for the group of links and name it Administration. Then we will add two links under the Administration header link. The links will link to the Create and Site Settings pages for the quicklaunch Site Collection.

SPSite quickLaunchSite = new SPSite("http://" + serverTextBox.Text + "/quicklaunch");

SPWeb quickLaunchWeb = quickLaunchSite.OpenWeb();

SPNavigationNodeCollection quickLaunchNodes = quickLaunchWeb.Navigation.QuickLaunch;

SPNavigationNode internalMenuItem = new SPNavigationNode("Administration", "", false);

quickLaunchNodes.AddAsFirst(internalMenuItem);

SPNavigationNode externalSubMenuItem1 = new SPNavigationNode("Site Settings", quickLaunchWeb.Url + "/_layouts/settings.aspx", true);

quickLaunchNodes[0].Children.AddAsFirst(externalSubMenuItem1);

SPNavigationNode externalSubMenuItem2 = new SPNavigationNode("Create", quickLaunchWeb.Url + "/_layouts/create.aspx", true);

quickLaunchNodes[0].Children.AddAsFirst(externalSubMenuItem2);

quickLaunchWeb.Update();

Top Navigation Menu Items

The top navigation menu may also be accessed programmatically. You can create new menu items in the top navigation menu navigation menu and remove them. You can also specify if the link is external to the site.

Here is how you add a menu item to the top navigation menu.

This example assumes you have created a top level site named topnavigation.

Once this Site Collection has been created its URLs will look like this: http://komal/topnavigation

In this example we will add two links to the top navigation menu for the topnavigation top level site, one link will be internal and one will be external. The internal link will point to the Links list in the topnavigation site. The external link will point to the SharePoint Experts web site.

SPSite topNavigationSite = new SPSite(“http://komal/topnavigation”);

SPWeb topNavigationWeb = topNavigationSite.OpenWeb();

SPNavigationNodeCollection topNavigationNodes = topNavigationWeb.Navigation.TopNavigationBar;

SPNavigationNode internalMenuItem = new SPNavigationNode(“Links”, “Lists/Links/AllItems.aspx”, false);

topNavigationNodes.AddAsFirst(internalMenuItem);

SPNavigationNode externalMenuItem = new SPNavigationNode(“SharePoint Data”, “http://SharePointdata.blogspot.com”, true);

topNavigationNodes.AddAsFirst(externalMenuItem);

topNavigationWeb.Update();