vrijdag 22 juni 2012

SharePoint 2010: Manage navigation programmatically C#

How to set navigation options programmatically?

  • Don't inherit global navigation
  • Don't include pages in global navigation
  • Include subsites in quicklaunch
  • Set ordering
  • Set navigation items of the topnavigation manually via xml
  • Add parent site to the quicklaunch if it exist

publishingWeb = PublishingWeb.GetPublishingWeb(currentWeb);

//Enable the navigation option to display only the current navigation not from the parent publishingWeb.Navigation.InheritGlobal = false;

//Don't include pages in global navigation publishingWeb.Navigation.GlobalIncludePages = false;

//Include subsites in quicklaunc publishingWeb.Navigation.CurrentIncludeSubSites = true;

//Set ordering to Sort pages automatically publishingWeb.Navigation.OrderingMethod = OrderingMethod.ManualWithAutomaticPageSorting;


Set navigation items of the topnavigation manually via xml

<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://telenet.be/intranet">
<NavBar Name="SharePoint Top Navbar" ID="1002">
<NavBarLink Name="$Resources:Telenet.SharePoint.Intranet,GlobalNavigation_Tine;" Url="$Resources:Telenet.SharePoint.Intranet,Portal_Tine_Url;/$Resources:osrvcore,List_Pages_UrlName;/default.aspx" />
<NavBarLink Name="$Resources:Telenet.SharePoint.Intranet,GlobalNavigation_MySite;" Url="http://my/" />

Code to parse the above XML

private bool ParseNavigation(string filename, SPWeb web)
XmlReader rdr = null;

ULS.Log.Debug(string.Format("Setting navigation on web '{0}' with file '{1}'"), web.Url, filename);
rdr = XmlReader.Create(filename, null);
XmlDocument nav = new XmlDocument();
XmlNamespaceManager mgr = new XmlNamespaceManager(nav.NameTable);
mgr.AddNamespace(string.Empty, http://telenet.be/intranet);
mgr.AddNamespace("ns", "http://telenet.be/intranet");

// Get all the navbars
foreach (XmlNode navbar in nav.SelectNodes("//ns:NavBar", mgr))
SPNavigationNode navBarNode = null;
int id = -1;
if (navbar.Attributes["ID"] != null && Int32.TryParse(navbar.Attributes["ID"].Value, out id))
navBarNode = web.Navigation.GetNodeById(id);

// Does the navbar exist?
if (navBarNode == null)
{string title = null, url = null, qs = null;
bool isExternal = true;
if (navbar.Attributes["Name"] != null)
title = // Method to read from Resource file (.resx)TelenetUtility.GetDeclarativeSiteResourceString(navbar.Attributes["Name"].Value, web);

if (navbar.Attributes["Url"] != null)
  url = TranslateUrl(navbar.Attributes["Url"].Value, web);

if (navbar.Attributes["IsExternal"] != null && !Boolean.TryParse(navbar.Attributes["IsExternal"].Value, out isExternal))
  isExternal = false;

if (url != null && url.IndexOf('?') != -1)
  qs = url.Substring(url.IndexOf('?') + 1);

navBarNode = new SPNavigationNode(title, url, isExternal);
if (!String.IsNullOrEmpty(qs))
navBarNode.Properties["UrlQueryString"] = qs;

// Enumerate the links
foreach (XmlNode navbarlink in navbar.SelectNodes("ns:NavBarLink", mgr))
string url = TranslateUrl(navbarlink.Attributes["Url"].Value, web);
// Get title from resource file if necessary
string title = TelenetUtility.GetDeclarativeSiteResourceString(navbarlink.Attributes["Name"].Value, web);
// Create the new navigation node
SPNavigationNode newNode;
if (url.Contains("://"))
  newNode = new SPNavigationNode(title, url, true);
  newNode = new SPNavigationNode(title, url, false);

catch (Exception ex)
ULS.Log.Error(ex, "An error occurred while configuring a navigation link");

foreach (XmlNode navbarProperty in navbar.SelectNodes("ns:Property", mgr))
string key = navbarProperty.Attributes["Key"].Value,
value = navbarProperty.Attributes["Value"].Value;
if (!String.IsNullOrEmpty(key) && !String.IsNullOrEmpty(value))
{if (!navBarNode.Properties.ContainsKey(key))
  navBarNode.Properties.Add(key, value);

catch (Exception ex)
ULS.Log.Error(ex, "An error occurred while configuring a navigation bar property");

return true;
catch (Exception ex)
ULS.Log.Error(ex, "An error occurred while configuring a navigation bar");
return false;
if (rdr != null && rdr.ReadState != ReadState.Closed)

Geen opmerkingen:

Een reactie posten