Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Security;
- using Microsoft.SharePoint.Client;
- namespace SPOSearchNav
- {
- class Program
- {
- private class Configuration
- {
- public static string ServiceSiteUrl = "https://<tenant>.sharepoint.com/Sites/<site>";
- public static string ServiceUserName = "<admin>@<tenant>.onmicrosoft.com";
- public static string ServicePassword = "<password>";
- }
- static ClientContext GetonlineContext()
- {
- var securePassword = new SecureString();
- foreach (char c in Configuration.ServicePassword)
- {
- securePassword.AppendChar(c);
- }
- var onlineCredentials = new SharePointOnlineCredentials(Configuration.ServiceUserName, securePassword);
- var context = new ClientContext(Configuration.ServiceSiteUrl);
- context.Credentials = onlineCredentials;
- return context;
- }
- static void Main(string[] args)
- {
- var clientContext = GetonlineContext();
- Web web = clientContext.Web;
- // Get the search navigation node collection.
- NavigationNode searchNav = web.Navigation.GetNodeById(1040);
- NavigationNodeCollection nodeCollection = searchNav.Children;
- clientContext.Load(nodeCollection);
- clientContext.ExecuteQuery();
- //delete all node in search navigation
- nodeCollection.ToList().ForEach(node => node.DeleteObject());
- clientContext.ExecuteQuery();
- // create
- // Set properties for a new navigation node.
- NavigationNodeCreationInformation node1 = new NavigationNodeCreationInformation();
- node1.Title = "This Site";
- node1.Url = "/sites/<site>/_layouts/15/osssearchresults.aspx?u={contexturl}";
- node1.AsLastNode = true; //be created as the last node in the collection
- nodeCollection.Add(node1);
- clientContext.Load(nodeCollection);
- clientContext.ExecuteQuery();
- NavigationNodeCreationInformation node2 = new NavigationNodeCreationInformation();
- node2.Title = "Everything";
- node2.Url = "https://<tenant>.sharepoint.com/search/Pages/results.aspx";
- node2.IsExternal = true;
- node2.AsLastNode = true; //be created as the last node in the collection
- //node2.PreviousNode = nodeCollection[0]; //be created after the first node in the navigation node collection
- nodeCollection.Add(node2);
- clientContext.Load(nodeCollection);
- clientContext.ExecuteQuery();
- NavigationNodeCreationInformation node3 = new NavigationNodeCreationInformation();
- node3.Title = "People";
- node3.Url = "https://<tenant>.sharepoint.com/search/Pages/peopleresults.aspx";
- node3.IsExternal = true;
- node3.AsLastNode = true; //be created as the last node in the collection
- //node3.PreviousNode = nodeCollection[1]; //be created after the second node in the navigation node collection
- nodeCollection.Add(node3);
- clientContext.Load(nodeCollection);
- clientContext.ExecuteQuery();
- foreach (NavigationNode navNode in nodeCollection)
- { Console.WriteLine(navNode.Title);}
- }
- }
- }
Add Comment
Please, Sign In to add comment