Oct 05 2016 10:47 AM
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?
Oct 06 2016 02:35 AM
SolutionPlease 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
Oct 06 2016 06:18 AM
Tanks Sanosh, Worked like a charm!
Apr 03 2017 02:10 PM
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?
Apr 03 2017 02:14 PM
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?
Oct 06 2016 02:35 AM
SolutionPlease 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