How To Create Structural Navigation Nodes In Quick Launch And Top Navigation

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 SharePoint.
 
In this blog, we are going to create a structural navigation node that is the root node having subnodes in both - top navigation and quick launch.
 
The code block for this is mentioned below.
  1. using System;  
  2. using System.Linq;  
  3. using System.Security;  
  4. using Microsoft.SharePoint.Client;  
  5. namespace StructuralNavigationNode {  
  6.     class Program {  
  7.         static void Main(string[] args) {  
  8.             ClientContext ctx = new ClientContext("http://portal/sites/site1");  
  9.             string password = "Password";  
  10.             SecureString secureString = new SecureString();  
  11.             foreach(char c in password.ToCharArray()) secureString.AppendChar(c);  
  12.             ctx.Credentials = new SharePointOnlineCredentials("[email protected]", secureString);  
  13.             ctx.ExecuteQuery();  
  14.             Web web = ctx.Web;  
  15.             NavigationNodeCollection quickLaunchNodeColl = web.Navigation.QuickLaunch; // we are getting all quick launch node collection  
  16.             NavigationNodeCollection topNavNodeColl = web.Navigation.TopNavigationBar; // we are getting all top navigation node collection  
  17.             NavigationNodeCreationInformation quickLaunchNodeCreation = new NavigationNodeCreationInformation();  
  18.             quickLaunchNodeCreation.Title = "QuickLaunchNode";  
  19.             quickLaunchNodeCreation.Url = "http://www.softreeconsulting.com/";  
  20.             quickLaunchNodeColl.Add(quickLaunchNodeCreation); //we are creating root quick launch node  
  21.             ctx.Load(quickLaunchNodeColl);  
  22.             ctx.ExecuteQuery();  
  23.             NavigationNode quickLaunchParentNode = quickLaunchNodeColl.Where(n => n.Title == "QuickLaunchNode").FirstOrDefault(); //retrieving parent quick launch root node for which we will add sub nodes  
  24.             NavigationNodeCreationInformation quickLaunchNodeCreationInformation = new NavigationNodeCreationInformation();  
  25.             quickLaunchNodeCreationInformation.Title = "QuickLaunchSubNode";  
  26.             quickLaunchNodeCreationInformation.Url = "http://www.softreeconsulting.com/about-us/";  
  27.             quickLaunchParentNode.Children.Add(quickLaunchNodeCreationInformation); //we are creating sub quicklaunch node  
  28.             quickLaunchParentNode.Update();  
  29.             ctx.ExecuteQuery();  
  30.             NavigationNodeCreationInformation topNavNodeCreation = new NavigationNodeCreationInformation();  
  31.             topNavNodeCreation.Title = "TopNavNode";  
  32.             topNavNodeCreation.Url = "http://www.softreeconsulting.com/";  
  33.             topNavNodeColl.Add(topNavNodeCreation); //we are creating root top navigation node  
  34.             ctx.Load(topNavNodeColl);  
  35.             ctx.ExecuteQuery();  
  36.             NavigationNode topNavParentNode = topNavNodeColl.Where(n => n.Title == "TopNavNode").FirstOrDefault(); //retrieving parent top navigation root node for which we will add sub nodes  
  37.             NavigationNodeCreationInformation topNavNodeCreationInformation = new NavigationNodeCreationInformation();  
  38.             topNavNodeCreationInformation.Title = "TopNavSubNode";  
  39.             topNavNodeCreationInformation.Url = "http://www.softreeconsulting.com/about-us/";  
  40.             topNavParentNode.Children.Add(topNavNodeCreationInformation); //we are creating sub top navigation node  
  41.             topNavParentNode.Update();  
  42.             ctx.ExecuteQuery();  
  43.         }  
  44.     }  
  45.  
Output
 
How To Create Structural Navigation Nodes Both In Quick Launch And Top Navigation Node 
 
After the code is executed, you can see the root navigation with its respective subnodes created successfully.