Wednesday, September 25, 2013

Sharepoint - Create Quick Launch menu programmatically

Quick Launch is navigation located on the left side of the screen and it is important part of almost every SharePoint site (marked in red square on the image). 

Quick Launch in SharePoint 2013

Links to the Quick Launch can be added manually in Site Settings, but, if you want to automate the process of adding the links, you can do it from c# code.

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


     using (SPSite oSiteCollection = new SPSite("http://mySiteUrl"))
           using (SPWeb oWeb = oSiteCollection.OpenWeb())
                 // Create the node.
                 SPNavigationNodeCollection _quickLaunchNav = oWeb.Navigation.QuickLaunch;
                 // Delete all existing items in Quick Launch
                 int _counter = 0;
                 while (_quickLaunchNav.Count != _counter && _quickLaunchNav.Count > 0)
                       SPNavigationNode _item = _quickLaunchNav[0];

                 // Here we read links that will go in Quick Launch 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);



In my next post, I'll show how to create custom links in Top Navigation of SharePoint. 

No comments:

Post a Comment