Thursday, July 12, 2012

Customize Top Navigation Programmatically in SharePoint 2010

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 2010:
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", "",true);

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

            SPNavigationNode oChild1 = new SPNavigationNode("Official", "");
            oChild1.Properties.Add("NodeType", "Heading");

            SPNavigationNode oChild2 = new SPNavigationNode("Leave Application","",true);
            oNewNode.Children.Add(oChild2, oChild1);

In an 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 SharePoint top navigation bar programmatically:

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

                    //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--)

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

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

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

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


You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.

Check out these SharePoint products:

1 comment :

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


Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...