Tuesday, October 1, 2013

SharePoint - Create Top Navigation menu programatically

In my previous post, I wrote about creating Quick Launch navigation programmatically . In this post, I will show how to create Top Navigation programmatically. It is easy since the code is almost the same as for Quick Launch.

Top Navigation is navigation located on the top of the screen and it is important part of almost every SharePoint site (marked in red square on the image). 

SharePoint Top Navigation


The code is very simple, it consists of reading the Top Navigation object and deleting the existing links in it. And then, after the existing Top Navigation is cleared, putting the new links which we have read from some text file.

C# CODE:



SPSecurity.RunWithElevatedPrivileges(delegate()
{
     using (SPSite oSiteCollection = new SPSite("http://mySiteUrl"))
     {
           using (SPWeb oWeb = oSiteCollection.OpenWeb())
           {
                 // Create the node.
                 SPNavigationNodeCollection _topNav = oWeb.Navigation.TopNavigationBar;
                 // Delete all existing items in Top Navigation
                 int _counter = 0;
                 while (_topNav.Count != _counter && _topNav.Count > 0)
                 {
                       _counter++;
                       SPNavigationNode _item = _topNav[0];
                       _item.Delete();
                 }

                 // Here we read links that will go in Top Navigation from text file
                 // Let's say that in "abc.txt" file we have links written as (title|link):
                 //
                 // My project |/sites/MyTestSite1
                 // Project documents|/sites/MyDocLib2
                 // Reports|/sites/MyCustomReports
                 //...
   
                 string _line;

                 System.IO.StreamReader _file = new System.IO.StreamReader("abc.txt");
                 while ((_line = _file.ReadLine()) != null)
                 {
                       string[] _splitStr = _line.Split('|'); 
                       string _title = _splitStr[0];
                       string _url = _splitStr[1];

                       SPNavigationNode _SPNode = new SPNavigationNode(_title, _url, true);
 
                       _topNav.AddAsLast(_SPNode);
                 }
                 _file.Close();
           }
     }
});

5 comments:

  1. OK I keep seeing a lot of useful items but no one tells you exactly how to implement it.
    So where would I put this code at? Step by Step please.

    ReplyDelete
    Replies
    1. The easiest way:
      1. Open Visual Studio
      2. Create new project --> Console Application
      3. Copy this code inside Main function
      4. Add reference to your project: "Microsoft.SharePoint.dll"
      5. In the code above change "http://mySiteUrl" to exact url of your SharePoint site
      6. Make sure you have file mentioned in code above --> "abc.txt" that contains your navigation links
      7. Hit "Run"

      This is short version, if you have any more questions, feel free to ask.

      Delete
    2. Hey Mario thank you very much for your post !!
      I m gonna make that with powershell using same APIs of course^^

      Alexandre, from France

      Delete
    3. Hi Alexandre, I am that it helped.

      Delete
  2. do u have any idea to implement navigation with sub navigation ?

    ReplyDelete