Skip to content

Instantly share code, notes, and snippets.

@star-crossed
Last active June 19, 2017 20:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save star-crossed/9a3414f84f066d1c41b397a222d9047f to your computer and use it in GitHub Desktop.
Save star-crossed/9a3414f84f066d1c41b397a222d9047f to your computer and use it in GitHub Desktop.
A PowerShell script SharePoint Online that will recursively sort the Quick Launch navigation for a web and its subwebs
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true, HelpMessage="This is the URL to the SharePoint Online site.")]
[string]$Url,
[Parameter(Mandatory=$true, HelpMessage="This is the ordering method: 0=Automatic, 1=Manual with Automatic Page Sorting, 2=Manual.")]
[ValidateSet(0, 1, 2)]
[int]$OrderingMethod,
[Parameter(Mandatory=$true, HelpMessage="This is the automatic sorting method: 0=Title, 1=Created Date, 2=Last Modified Date.")]
[ValidateSet(0, 1, 2)]
[int]$AutomaticSortingMethod,
[Parameter(Mandatory=$true, HelpMessage="This is to sort the navigation nodes in ascending order.")]
[bool]$SortAscending,
[Parameter(Mandatory=$true, HelpMessage="This is the path to the DLLs for CSOM.")]
[string]$CSOMPath
)
Set-Strictmode -Version 1
If ($CSOMPath -eq $null -or $CSOMPath -eq "") { $CSOMPath = "." }
Add-Type -Path "$CSOMPath\Microsoft.SharePoint.Client.dll"
Add-Type -Path "$CSOMPath\Microsoft.SharePoint.Client.Runtime.dll"
function getWeb ([Microsoft.SharePoint.Client.Web]$currentWeb) {
$clientContext.Load($currentWeb)
$clientContext.Load($currentWeb.AllProperties)
$clientContext.Load($currentWeb.Webs)
$clientContext.ExecuteQuery()
Write-Host $currentWeb.Title: $currentWeb.Url
$currentWeb.AllProperties["__NavigationOrderingMethod"] = $OrderingMethod
$currentWeb.AllProperties["__NavigationAutomaticSortingMethod"] = $AutomaticSortingMethod
$currentWeb.AllProperties["__NavigationSortAscending"] = $SortAscending
$currentWeb.Update()
$clientContext.ExecuteQuery()
$currentWeb.Webs | ForEach-Object {
getWeb($_)
}
}
$psCredentials = Get-Credential
$spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($psCredentials.UserName, $psCredentials.Password)
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$clientContext.Credentials = $spoCredentials
If ($clientContext.ServerObjectIsNull.Value) {
Write-Error "Could not connect to SharePoint Online site collection: $Url"
} Else {
Write-Host "Connected to SharePoint Online site collection: " $Url -ForegroundColor Green
$web = $clientContext.Web
getWeb($web)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment