2

I'm trying to automate the process of creating a subsite using PowerShell, but I can't figure out how to create TopNavigaiton link.

The problem is that SPWeb.Navigation.TopNavigationBar is null when I'm working in PowerShell.

I've heard that in order to force SPWeb.Navigation.TopNavigationBar not be null, we can go to the navigation page in the browser and click "Apply". But This is not a solution since I want to automate this step in PowerShell.

This my current code:

$wikiUrl = $web.Url + "/WIki" 
New-SPWeb -Url $wikiUrl -Template "STS#0" -Name "Wiki" -Description "" 
$wikiWeb = Get-SPWeb $wikiUrl

$BackToRootNavigationNode = New-Object Microsoft.SharePoint.Navigation.SPNavigationNode("Back to home page ", $null, $true)
# $wikiWeb.Navigation.TopNavigationBar is $null here: 
$wikiWeb.Navigation.TopNavigationBar.AddAsLast($BackToRootNavigationNode)
$BackToRootNavigationNode.Url  = $wikiWeb.site.ServerRelativeUrl
$BackToRootNavigationNode.Update()

This is what Navigation returns:

Web              : Wiki
Home             : 
GlobalNodes      : {1025, 1040, 2004}
TopNavigationBar : 
QuickLaunch      : {1031, 2001, 1034}
SearchNav        : {}
UseShared        : False

As you can see TopNavigationBar property does not contain anything. It's null

Update:

I've found this Waldek Mastykarz's article:

The first time that you try to get the value of the PublishingWeb.Navigation.GlobalNavigationNodes property, the returned collection is empty. Even though the menu on your site properly displays the whole structure on your site, no nodes are being returned in the code!

Screenshot of a site and code. Although menu items are properly displayed on the site, code doesn’t return any menu items

In fact, the collection of menu items remains empty until you change the navigation settings in the Site Actions > Site Settings > Look and Feel > Navigation options.

But it was SP2010. I wonder if there is a workaround in SharePoint 2013?

Your Answer

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

Browse other questions tagged or ask your own question.