SOLVED

PnPPowershell Add-SPONavigationNode question

Iron Contributor

Hi,

I am trying to use the PnPPowershell Add-SPONavigationNode  command to  add all of a webs subwebs to its Quicklaunch. The Site is on Office 365. I am attempting to do this recursively, so i just run the script on the rootweb,, and it sill set this up for all the subwebs.

 

My powershell script is:

function fixupNavigation ($web) {

 

Add-SPONavigationNode -Web $web -Location QuickLaunch -Title "Teams"

$subwebs=$web.Webs

$context.Load($subwebs)

$context.ExecuteQuery()

foreach ($subweb in $subwebs){

Add-SPONavigationNode -Web $web -Location QuickLaunch -Title $subweb.Title -Url $subweb.Url -Header "Teams"

}

foreach ($subweb in $subwebs){

fixupNavigation $subweb

}

}

Connect-SPOnline -Url https://tenant.sharepoint.com/teams/IT -Credentials o365

$context=get-spocontext

$web=$context.Web

fixupNavigation $web

 

It successfully adds all the subsites to the  root-web, but as soon as it goies to the first subweb, it gets an error on line

 

Add-SPONavigationNode -Web $web -Location QuickLaunch -Title "Teams"  

the error says

Add-SPONavigationNode : The object is used in the context different from the one associated with the object.

Anybody know how to fix thi?

 

4 Replies
best response confirmed by Russell Gove (Iron Contributor)
Solution

Please try this script.

 

function fixupNavigation ($web) {

Add-SPONavigationNode -Web $web -Location QuickLaunch -Title "Teams"
$subwebs=$web.Webs
$context.Load($subwebs)
$context.ExecuteQuery()
foreach ($subweb in $subwebs){
Add-SPONavigationNode -Web $web -Location QuickLaunch -Title $subweb.Title -Url $subweb.Url -Header "Teams"
}
foreach ($subweb in $subwebs){
Connect-SPOnline -Url $subweb.Url -Credentials $o365
$context=get-spocontext
$subconweb=$context.Web
fixupNavigation $subconweb
}
}
$o365=Get-Credential
Connect-SPOnline –Url https://tenant.sharepoint.com/teams/IT -Credentials $o365
$context=get-spocontext
$web=$context.Web
fixupNavigation $web 

Tanks Sanosh, Worked like a charm!

Hi Santosh,

The sample you provoided worked fine back in October. I'm coming back to this project and the sample has stopped working. I have updated to the most recent SharePointPnPPowershell (2.13.1703) thinking there may have been some changes , but I'm still getting the error:

Add-PNPNavigationNode : The object is used in the context different from the one associated with the object.

I have updated the code to use the new PNP-named cmdlets as follows:

function fixupNavigation ($web) {

    Add-PNPNavigationNode -Web $web -Location QuickLaunch -Title "Teams"
    $subwebs=$web.Webs
    $context.Load($subwebs)
    $context.ExecuteQuery()
    foreach ($subweb in $subwebs){
        Add-PNPNavigationNode -Web $web -Location QuickLaunch -Title $subweb.Title -Url $subweb.Url -Header "Teams"
    }
    foreach ($subweb in $subwebs){
        Connect-PNPOnline -Url $subweb.Url -Credentials $o365
        $context=get-spocontext
        $subconweb=$context.Web
        fixupNavigation $subconweb
    }
}

Connect-pnpOnline -Url https://tronoxglobal.sharepoint.com/teams/IT/ -Credentials o365
$context=get-pnpcontext 
$web=$context.Web 

fixupNavigation $web 

 

It throws the

Add-PNPNavigationNode : The object is used in the context different from the one associated with the object.

error at  the line

Add-PNPNavigationNode -Web $web -Location QuickLaunch -Title "Teams"

 

This error is now occurring at the very first execution of the fixupNavigation method (the call for the rootweb)

 

Any idea how to fix this?

Hi Santosh,

The sample you provoided worked fine back in October. I'm coming back to this project and the sample has stopped working. I have updated to the most recent SharePointPnPPowershell (2.13.1703) thinking there may have been some changes , but I'm still getting the error:

Add-PNPNavigationNode : The object is used in the context different from the one associated with the object.

I have updated the code to use the new PNP-named cmdlets as follows:

function fixupNavigation ($web) {

    Add-PNPNavigationNode -Web $web -Location QuickLaunch -Title "Teams"
    $subwebs=$web.Webs
    $context.Load($subwebs)
    $context.ExecuteQuery()
    foreach ($subweb in $subwebs){
        Add-PNPNavigationNode -Web $web -Location QuickLaunch -Title $subweb.Title -Url $subweb.Url -Header "Teams"
    }
    foreach ($subweb in $subwebs){
        Connect-PNPOnline -Url $subweb.Url -Credentials $o365
        $context=get-spocontext
        $subconweb=$context.Web
        fixupNavigation $subconweb
    }
}

Connect-pnpOnline -Url https://tronoxglobal.sharepoint.com/teams/IT/ -Credentials o365
$context=get-pnpcontext 
$web=$context.Web 

fixupNavigation $web 

 

It throws the

Add-PNPNavigationNode : The object is used in the context different from the one associated with the object.

error at  the line

Add-PNPNavigationNode -Web $web -Location QuickLaunch -Title "Teams"

 

This error is now occurring at the very first execution of the fixupNavigation method (the call for the rootweb)

 

Any idea how to fix this?

1 best response

Accepted Solutions
best response confirmed by Russell Gove (Iron Contributor)
Solution

Please try this script.

 

function fixupNavigation ($web) {

Add-SPONavigationNode -Web $web -Location QuickLaunch -Title "Teams"
$subwebs=$web.Webs
$context.Load($subwebs)
$context.ExecuteQuery()
foreach ($subweb in $subwebs){
Add-SPONavigationNode -Web $web -Location QuickLaunch -Title $subweb.Title -Url $subweb.Url -Header "Teams"
}
foreach ($subweb in $subwebs){
Connect-SPOnline -Url $subweb.Url -Credentials $o365
$context=get-spocontext
$subconweb=$context.Web
fixupNavigation $subconweb
}
}
$o365=Get-Credential
Connect-SPOnline –Url https://tenant.sharepoint.com/teams/IT -Credentials $o365
$context=get-spocontext
$web=$context.Web
fixupNavigation $web 

View solution in original post