Tuesday, September 27, 2011

SharePoint Working with QuickLaunch Programmatically

Working with QuickLaunch Programmatically.

In QuickLaunch navigation structure, the tree is rooted on a single SPNavigationNode object. This object has a Children property that returns an SPNavigationNodeCollection object with navigation nodes one level down from the root. Each of these nodes has a Children property that can contain a collection of nodes for another level down the hierarchy.
You can get the parent node for a collection by getting the value of the collection's Parent property. You can return to the collection by getting the value of the parent node's Children property.

To add QuickLaunch Root and Children programmatically use code as described below:

public static void AddQuickLaunchItem(string header, string item, string url)
using(SPSite site = new SPSite("
using(SPWeb web = site.OpenWeb())

SPNavigationNodeCollection quickLaunch = web.Navigation.QuickLaunch;

// try to get quick launch header
SPNavigationNode nodeHeader =
quickLaunch.Cast<SPNavigationNode>().Where(n => n.Title == header).FirstOrDefault();

//if header not found create it
if (nodeHeader == null)
    nodeHeader = quickLaunch.AddAsFirst(new SPNavigationNode(header, ""));


//try to get node item under header
SPNavigationNode nodeItem =
      nodeHeader.Children.Cast<SPNavigationNode>().Where(n => n.Title == item).FirstOrDefault();

//If item not found under heading then create it
if (nodeItem == null)
      nodeItem = nodeHeader.Children.AddAsLast(new SPNavigationNode(item, url));
      nodeItem.Url = url;



1 comment:

Popular Posts