Advertisement
Guest User

Untitled

a guest
Jan 19th, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. $siteUrl = $_.Url
  2. $siteTitle = $_.Title
  3. $context = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
  4. $context.Credentials = $credentials
  5. $context.RequestTimeOut = 5000 * 60 * 10;
  6. $web = $context.Web
  7. $site = $context.Site
  8. $context.Load($web)
  9. $context.Load($site)
  10. $context.ExecuteQuery()
  11.  
  12. $NavBar = $context.Web.Navigation.TopNavigationBar
  13. $NavigationNode = New-Object Microsoft.SharePoint.Client.NavigationNodeCreationInformation
  14. $NavigationNode.Title = $ItemName
  15. $NavigationNode.Url = $ItemUrlPath
  16. $NavigationNode.AsLastNode = $true
  17. $context.Load($NavBar.Add($NavigationNode))
  18. $context.ExecuteQuery()
  19.  
  20. Add-Type -Path "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.dll"
  21. Add-Type -Path "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.Runtime.dll"
  22.  
  23.  
  24. Function Get-SPOContext([string]$Url,[string]$UserName,[string]$Password)
  25. {
  26. $context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
  27. $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
  28. $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
  29. $context.Credentials = $credentials
  30. return $context
  31. }
  32.  
  33.  
  34. Function FindNavigationNodeByTitle([Microsoft.SharePoint.Client.NavigationNodeCollection]$Nodes,[string]$Title)
  35. {
  36. $context = $Nodes.Context
  37. $context.Load($Nodes)
  38. $context.ExecuteQuery()
  39. $node = $Nodes | Where-Object { $_.Title -eq $Title }
  40. return $node
  41. }
  42.  
  43.  
  44. Function AddNavigationNode([Microsoft.SharePoint.Client.NavigationNodeCollection]$Nodes,[string]$Title,[string]$Url){
  45. $context = $Nodes.Context
  46. $Node = New-Object Microsoft.SharePoint.Client.NavigationNodeCreationInformation
  47. $Node.Title = $Title
  48. $Node.Url = $Url
  49. $Node.AsLastNode = $true
  50. $context.Load($Nodes.Add($Node))
  51. $context.ExecuteQuery()
  52. }
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60. $tenantUrl = "https://tenant.sharepoint.com/"
  61. $userName = "username@tenant.onmicrosoft.com"
  62. $password = "password"
  63.  
  64.  
  65.  
  66. $Context = Get-SPOContext -Url $tenantUrl -UserName $userName -Password $password
  67.  
  68. #$NavBar = GetTopNavBar -Context $Context
  69. $NavBar = $Context.Web.Navigation.TopNavigationBar
  70. $Context.Load($NavBar)
  71. $Context.ExecuteQuery()
  72.  
  73.  
  74. $parentNode = FindNavigationNodeByTitle -Nodes $NavBar -Title "Hello"
  75.  
  76. if($parentNode) {
  77. $Context.Load($parentNode.Children)
  78. $Context.ExecuteQuery()
  79. AddNavigationNode -Context $Nodes $parentNode.Children -Title "Tasks" -Url "/Lists/Tasks/AllItems.aspx"
  80. }
  81.  
  82. $Context.Dispose()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement