Guest User

Untitled

a guest
Aug 31st, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Security;
  6. using Microsoft.SharePoint.Client;
  7.  
  8. namespace SPOSearchNav
  9. {
  10. class Program
  11. {
  12. private class Configuration
  13. {
  14. public static string ServiceSiteUrl = "https://<tenant>.sharepoint.com/Sites/<site>";
  15. public static string ServiceUserName = "<admin>@<tenant>.onmicrosoft.com";
  16. public static string ServicePassword = "<password>";
  17. }
  18.  
  19. static ClientContext GetonlineContext()
  20. {
  21. var securePassword = new SecureString();
  22. foreach (char c in Configuration.ServicePassword)
  23. {
  24. securePassword.AppendChar(c);
  25. }
  26. var onlineCredentials = new SharePointOnlineCredentials(Configuration.ServiceUserName, securePassword);
  27. var context = new ClientContext(Configuration.ServiceSiteUrl);
  28. context.Credentials = onlineCredentials;
  29. return context;
  30. }
  31.  
  32. static void Main(string[] args)
  33. {
  34. var clientContext = GetonlineContext();
  35. Web web = clientContext.Web;
  36.  
  37. // Get the search navigation node collection.
  38. NavigationNode searchNav = web.Navigation.GetNodeById(1040);
  39. NavigationNodeCollection nodeCollection = searchNav.Children;
  40. clientContext.Load(nodeCollection);
  41. clientContext.ExecuteQuery();
  42.  
  43. //delete all node in search navigation
  44. nodeCollection.ToList().ForEach(node => node.DeleteObject());
  45. clientContext.ExecuteQuery();
  46.  
  47. // create
  48. // Set properties for a new navigation node.
  49. NavigationNodeCreationInformation node1 = new NavigationNodeCreationInformation();
  50. node1.Title = "This Site";
  51. node1.Url = "/sites/<site>/_layouts/15/osssearchresults.aspx?u={contexturl}";
  52. node1.AsLastNode = true; //be created as the last node in the collection
  53. nodeCollection.Add(node1);
  54. clientContext.Load(nodeCollection);
  55. clientContext.ExecuteQuery();
  56.  
  57. NavigationNodeCreationInformation node2 = new NavigationNodeCreationInformation();
  58. node2.Title = "Everything";
  59. node2.Url = "https://<tenant>.sharepoint.com/search/Pages/results.aspx";
  60. node2.IsExternal = true;
  61. node2.AsLastNode = true; //be created as the last node in the collection
  62. //node2.PreviousNode = nodeCollection[0]; //be created after the first node in the navigation node collection
  63. nodeCollection.Add(node2);
  64. clientContext.Load(nodeCollection);
  65. clientContext.ExecuteQuery();
  66.  
  67. NavigationNodeCreationInformation node3 = new NavigationNodeCreationInformation();
  68. node3.Title = "People";
  69. node3.Url = "https://<tenant>.sharepoint.com/search/Pages/peopleresults.aspx";
  70. node3.IsExternal = true;
  71. node3.AsLastNode = true; //be created as the last node in the collection
  72. //node3.PreviousNode = nodeCollection[1]; //be created after the second node in the navigation node collection
  73. nodeCollection.Add(node3);
  74. clientContext.Load(nodeCollection);
  75. clientContext.ExecuteQuery();
  76.  
  77. foreach (NavigationNode navNode in nodeCollection)
  78. { Console.WriteLine(navNode.Title);}
  79.  
  80. }
  81. }
  82. }
Add Comment
Please, Sign In to add comment