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 = PublishingWeb.GetPublishingWeb(currentWeb);
Set navigation items of the topnavigation manually via xml
XML
<?xml version="1.0" encoding="utf-8" ?>Code to parse the above XML
private bool ParseNavigation(string filename, SPWeb web)
XmlReader rdr = null;
try
{
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();
nav.Load(rdr);
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);
web.Navigation.QuickLaunch.AddAsLast(navBarNode);
if (!String.IsNullOrEmpty(qs))
navBarNode.Properties["UrlQueryString"] = qs;
}
// Enumerate the links
foreach (XmlNode navbarlink in navbar.SelectNodes("ns:NavBarLink", mgr))
{
try
{
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);
else
newNode = new SPNavigationNode(title, url, false);
navBarNode.Children.AddAsLast(newNode);
}
catch (Exception ex)
{
ULS.Log.Error(ex, "An error occurred while configuring a navigation link");
}
}
foreach (XmlNode navbarProperty in navbar.SelectNodes("ns:Property", mgr))
{
try
{
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");
}
}
navBarNode.Update();
}
return true;
}
catch (Exception ex)
{
ULS.Log.Error(ex, "An error occurred while configuring a navigation bar");
return false;
}
finally
{
if (rdr != null && rdr.ReadState != ReadState.Closed)
rdr.Close();
}
}
Geen opmerkingen:
Een reactie posten