How to create structural navigation nodes both in quick launch and top navigation node

  • Posted by: Softree Technology

Sharepoint customizes navigation feature so that user can navigate to a different location and access them quickly. You can add, edit, or remove links on the left-hand menu (Quick Launch menu), the top menu (Top Navigation Node) through edit links.

In this blog, we are going to create structural navigation node that is root node having subnodes in both top navigation and quick launch.

using System;
using System.Linq;
using System.Security;
using Microsoft.SharePoint.Client;
namespace StructuralNavigationNode
{
class Program
{
static void Main(string[] args)
{
ClientContext ctx = new ClientContext("http://portal/sites/site1");
string password = "Password";
SecureString secureString = new SecureString();
foreach (char c in password.ToCharArray()) secureString.AppendChar(c);
ctx.Credentials = new SharePointOnlineCredentials("user@domain.com", secureString);
ctx.ExecuteQuery();
Web web = ctx.Web;

NavigationNodeCollection quickLaunchNodeColl = web.Navigation.QuickLaunch; // we are getting all quick launch node collection
NavigationNodeCollection topNavNodeColl = web.Navigation.TopNavigationBar; // we are getting all top navigation node collection

NavigationNodeCreationInformation quickLaunchNodeCreation = new NavigationNodeCreationInformation();
quickLaunchNodeCreation.Title = "QuickLaunchNode";
quickLaunchNodeCreation.Url = "http://www.softreeconsulting.com/";
quickLaunchNodeColl.Add(quickLaunchNodeCreation); //we are creating root quick launch node
ctx.Load(quickLaunchNodeColl);
ctx.ExecuteQuery();

NavigationNode quickLaunchParentNode = quickLaunchNodeColl.Where(n => n.Title == "QuickLaunchNode").FirstOrDefault(); //retrieving parent quick launch root node for which we will add sub nodes
NavigationNodeCreationInformation quickLaunchNodeCreationInformation = new NavigationNodeCreationInformation();
quickLaunchNodeCreationInformation.Title = "QuickLaunchSubNode";
quickLaunchNodeCreationInformation.Url = "http://www.softreeconsulting.com/about-us/";
quickLaunchParentNode.Children.Add(quickLaunchNodeCreationInformation); //we are creating sub quicklaunch node
quickLaunchParentNode.Update();
ctx.ExecuteQuery();

NavigationNodeCreationInformation topNavNodeCreation = new NavigationNodeCreationInformation();
topNavNodeCreation.Title = "TopNavNode";
topNavNodeCreation.Url = "http://www.softreeconsulting.com/";
topNavNodeColl.Add(topNavNodeCreation); //we are creating root top navigation node
ctx.Load(topNavNodeColl);
ctx.ExecuteQuery();

NavigationNode topNavParentNode = topNavNodeColl.Where(n => n.Title == "TopNavNode").FirstOrDefault(); //retrieving parent top navigation root node for which we will add sub nodes
NavigationNodeCreationInformation topNavNodeCreationInformation = new NavigationNodeCreationInformation();
topNavNodeCreationInformation.Title = "TopNavSubNode";
topNavNodeCreationInformation.Url = "http://www.softreeconsulting.com/about-us/";
topNavParentNode.Children.Add(topNavNodeCreationInformation); //we are creating sub top navigation node
topNavParentNode.Update();
ctx.ExecuteQuery();

}

}
}

After code executed, you can see root navigation with their respective subnodes created successfully.

This solution is brought to you by our SharePoint professionals.

Softree Technology employs SharePoint consultants; we are a technology services provider with the aim to help companies achieve exceptional performance through SharePoint. Our dedicated team of SharePoint consultants has the right bent of mind to understand and execute customer requirements.

Be it SPFx or SharePoint add-in developments, SharePoint 2019 developments, web part developments, migrating from SharePoint 2010/2013 to SharePoint 2013/2016/Office 365, Office 365, SharePoint hosted apps development or something else in SharePoint, we strive to deliver the best

Author: Softree Technology

Leave a Reply