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

3
  • Remove $web.Update() and check ? Its not needed Commented Jul 3, 2018 at 16:25
  • can you change the URL to $newNavNode.Url = $siteUrl + "External Documents/Forms/AllItems.aspx" and check ? Commented Jul 4, 2018 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)' Commented Jul 4, 2018 at 10:41

1 Answer 1

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()
1
  • 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 Commented Jul 4, 2018 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.