0

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

  • Remove $web.Update() and check ? Its not needed – Gautam Sheth Jul 3 '18 at 16:25
  • can you change the URL to $newNavNode.Url = $siteUrl + "External Documents/Forms/AllItems.aspx" and check ? – Gautam Sheth Jul 4 '18 at 9:26
  • Still the same... If I run it line by line, it is when I get to this part that I see the initialization error: '$navColl.Add($newNavNode)' – Tom Urwin Jul 4 '18 at 10:41
1

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()
  • cheers, happy to help ! :) You can mark it as answer by ticking the checkmark besides the answer. You can refer how to do that here – Gautam Sheth Jul 4 '18 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.