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.
- 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("[email protected]", secureString);
- ctx.ExecuteQuery();
- Web web = ctx.Web;
- NavigationNodeCollection quickLaunchNodeColl = web.Navigation.QuickLaunch;
- NavigationNodeCollection topNavNodeColl = web.Navigation.TopNavigationBar;
- NavigationNodeCreationInformation quickLaunchNodeCreation = new NavigationNodeCreationInformation();
- quickLaunchNodeCreation.Title = "QuickLaunchNode";
- quickLaunchNodeCreation.Url = "http://www.softreeconsulting.com/";
- quickLaunchNodeColl.Add(quickLaunchNodeCreation);
- ctx.Load(quickLaunchNodeColl);
- ctx.ExecuteQuery();
- NavigationNode quickLaunchParentNode = quickLaunchNodeColl.Where(n => n.Title == "QuickLaunchNode").FirstOrDefault();
- NavigationNodeCreationInformation quickLaunchNodeCreationInformation = new NavigationNodeCreationInformation();
- quickLaunchNodeCreationInformation.Title = "QuickLaunchSubNode";
- quickLaunchNodeCreationInformation.Url = "http://www.softreeconsulting.com/about-us/";
- quickLaunchParentNode.Children.Add(quickLaunchNodeCreationInformation);
- quickLaunchParentNode.Update();
- ctx.ExecuteQuery();
- NavigationNodeCreationInformation topNavNodeCreation = new NavigationNodeCreationInformation();
- topNavNodeCreation.Title = "TopNavNode";
- topNavNodeCreation.Url = "http://www.softreeconsulting.com/";
- topNavNodeColl.Add(topNavNodeCreation);
- ctx.Load(topNavNodeColl);
- ctx.ExecuteQuery();
- NavigationNode topNavParentNode = topNavNodeColl.Where(n => n.Title == "TopNavNode").FirstOrDefault();
- NavigationNodeCreationInformation topNavNodeCreationInformation = new NavigationNodeCreationInformation();
- topNavNodeCreationInformation.Title = "TopNavSubNode";
- topNavNodeCreationInformation.Url = "http://www.softreeconsulting.com/about-us/";
- topNavParentNode.Children.Add(topNavNodeCreationInformation);
- topNavParentNode.Update();
- ctx.ExecuteQuery();
- }
- }
- }
Output
After the code is executed, you can see the root navigation with its respective subnodes created successfully.