Customize Top Navigation Programmatically in SharePoint

Requirement: Had to customize the Top navigation with bunch of provided internal/external links as part of a branding project.

Solution: Designed a feature to programmatically add links to top navigation bar. when activated, feature will add bunch of links to the top navigation bar.

Customize navigation programmatically in SharePoint:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{ 
	SPSite site = properties.Feature.Parent as SPSite;
	SPWeb rootWeb = site.RootWeb;
	// Get the top link bar.
	SPNavigationNodeCollection topNavNodes = rootWeb.Navigation.TopNavigationBar;

	// Create a simple external Link node.
	SPNavigationNode node = new SPNavigationNode("I-Best", "http://ibest.crescent.com",true);
	topNavNodes.AddAsLast(node);

	//Add Heading & child Links node
	SPNavigationNode oNewNode = new SPNavigationNode("Org Browser", "");
	rootWeb.Navigation.TopNavigationBar.AddAsLast(oNewNode);
	oNewNode.Properties.Add("NodeType", "Heading");
	oNewNode.Update();

	SPNavigationNode oChild1 = new SPNavigationNode("Official", "");
	oNewNode.Children.AddAsFirst(oChild1);
	oChild1.Properties.Add("NodeType", "Heading");
	oChild1.Update();

	SPNavigationNode oChild2 = new SPNavigationNode("Leave Application","http://ibest.crescent.com/apps/Leave.aspx",true);
	oNewNode.Children.Add(oChild2, oChild1);
 }

In another SharePoint 2007 Project, Had to remove all the Top navigation items and add links for custom provisioned pages. Here is the code for customizing the SharePoint top navigation bar programmatically:

//Use Unique Top navigation 
web.Navigation.UseShared = false;
web.Update();


//Get the top navigation to customize
SPNavigationNodeCollection topNavigationBarNodes = web.Navigation.TopNavigationBar;

//Remove All Nodes in Top navigation
for (int i = topNavigationBarNodes.Count; i > 0; i--)
{
	topNavigationBarNodes.Delete(topNavigationBarNodes[i-1]);
}

//Add Top Navigation Page Links
SPNavigationNode HomePageMenuItem = new SPNavigationNode(web.Title, "Main.aspx", false);
topNavigationBarNodes.AddAsFirst(HomePageMenuItem);

//Add Project Documents Link
SPNavigationNode ProjectDocumentsMenuItem = new SPNavigationNode("Project Documents","ProjectDocuments.aspx", false);
topNavigationBarNodes.AddAsLast(ProjectDocumentsMenuItem);

//Add Project Accounting
SPNavigationNode ProjectAccountingMenuItem = new SPNavigationNode("Project Accounting", "ProjectAccounting.aspx", false);
topNavigationBarNodes.AddAsLast(ProjectAccountingMenuItem);

//Add Project Checklist
SPNavigationNode ProjectChecklistMenuItem = new SPNavigationNode("Project Checklist", "ProjectChecklist.aspx", false);
topNavigationBarNodes.AddAsLast(ProjectChecklistMenuItem);

web.Update(); 

Salaudeen Rajack

Information Technology Professional with Two decades of SharePoint Experience.

One thought on “Customize Top Navigation Programmatically in SharePoint

  • August 23, 2015 at 11:51 PM

    Hi there Just wondering if you know a way that this could be achieved with CSOM, maybe in a console app.
    thanks

    Reply

Leave a Reply