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();
}
}
}