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", "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 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;
                    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(); 



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


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.
    thanks

    ReplyDelete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...