SPO - Add Document Library to Quick Launch using Powershell
-
19-01-2021 - |
Domanda
As part of an SPO site creation script, I'm trying to get newly created document libraries automatically added to the Quick Launch navigation links.
I'm running this code:
$SPOCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName,$password)
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$Ctx.Credentials = $SPOCredentials
$web = $Ctx.Web
$navColl = $web.Navigation.QuickLaunch
$newNavNode = New-Object
Microsoft.SharePoint.Client.NavigationNodeCreationInformation
$newNavNode.Title = "External Documents"
$newNavNode.Url = "External Documents/Forms/AllItems.aspx"
$newNavNode.AsLastNode = $false
$navColl.Add($newNavNode)
$web.Update()
$Ctx.Load($navColl)
$Ctx.ExecuteQuery()
But it's returning this:
format-default : The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.
+ CategoryInfo : NotSpecified: (:) [format-default], CollectionNotInitializedException
+ FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException,Microsoft.PowerShell.Commands.FormatDefaultCommand
Any ideas please?
Thanks
Soluzione
You also need to initialise the Navigation Node collection.
Just add $Ctx.Load($navColl.Add($newNavNode))
to the script
Modify your code as below so that it looks something like :
$web = $Ctx.Web
$navColl = $web.Navigation.QuickLaunch
$newNavNode = New-Object Microsoft.SharePoint.Client.NavigationNodeCreationInformation
$newNavNode.Title = "External Documents"
$newNavNode.Url = $siteUrl + "External Documents/Forms/AllItems.aspx"
$newNavNode.AsLastNode = $false
$Ctx.Load($navColl.Add($newNavNode))
$Ctx.ExecuteQuery()
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange