Tag Archives: Navigation

Add custom link to structural navigation menu

Recently I had to add custom link to structural navigation menu in SharePoint 2013. After searching on over the net I found noting and tried to check what properties should be set inside navigation provider by craeting manually link in SharePoint navigation and checking result with debugger…

Here are properties that should be set to be able to insert custom link node in global navigation provider:

ndParent.Properties["NodeType"] = "AuthoredLinkPlain";
ndParent.Properties["vti_navsequencechild"] = true;

  • NodeType property indicates that the node is a custom link.
  • vti_navsequencechild property makes link to appear inside the navigation provider

Here is a full peace of code to add link:

private void SaveNavigationNode(string title, string url)
{
if (string.IsNullOrEmpty(title)) return;
if (string.IsNullOrEmpty(url)) return;

var web = SPContext.Current.Web;

web.AllowUnsafeUpdates = true;

// Get the top link bar.
var topnav = web.Navigation.TopNavigationBar;

var existingNode = topnav.Cast().FirstOrDefault(n => n.Title.ToLowerInvariant().Equals(title.ToLowerInvariant()) ||
n.Url.ToLowerInvariant().Equals(url.ToLowerInvariant()));

if (existingNode == null)
{
var ndParent = topnav.AddAsFirst(new SPNavigationNode(title, url, true)); // Add to collection.
ndParent.Properties["NodeType"] = "AuthoredLinkPlain";
ndParent.Properties["vti_navsequencechild"] = true;
ndParent.Update();
}
else
{
existingNode.Title = title;
existingNode.Url = url;
existingNode.Update();
}

web.AllowUnsafeUpdates = false;
}

Hope this helps,

Andrew Adamich

Advertisements