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", "https://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","https://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

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

One thought on “Customize Top Navigation Programmatically in SharePoint

  • 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

Your email address will not be published. Required fields are marked *