Configuring your site settings is really something that you want to do using PowerShell, this is for many reasons but the most obvious ones are that you get more control. You can test your script with settings in a test environment and see the effect, when it is perfected you simply run the script against a different URL and the result is moved to the proper target environment.
One more benefit is repeatability, you can do one configuration and repeat it for every site or web in your farm. Doing the same thing manually would take time and would increase the risk of making mistakes.
(See more Site settings by Powershell in my Whitepaper that can be downloaded from here: SharePoint 2010 Site Settings explained. I have tried to cover most every site setting and how it can be done using Powershell only. )
All of these settings can also be configured during site creation, in order to do that, get your publishingweb object of the websites you are creating, set the values and same as always, finish off with an update().
In this post I will try to cover most of the settings you can do in the Navigation part of your site settings. The Navigation settings are available only in sites in a site collection that has the SharePoint Server Publishing Infrastructure Site Collection Feature activated.
Activating this feature gives you among many other things, control over the global and current navigations. These settings are available in Site Settings under Look and Feel.
The settings that I will show how to configure using PowerShell are as shown in the below image. There are also ways to add and remove items from the navigations in the same dialog, but these will not be covered here.
First off, we need an object to work with. In this case we need a Publishing Web object, so first we create a spweb object, then a publishing web object:
$SPWeb = Get-SPSPWeb http://server/sitecoll/spweb/spweb $SPPubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb] ::GetPublishingWeb($SPWeb)
Some changes, like unchecking the Show subsites, require that you first allow unsafe updates. You can set the show subsites to false, but it will never be reflected unless you first allow unsafe updates. This is done on the SPWeb object by:
$SPWeb.AllowUnsafeUpdates = $true
Using our Publishing Web object, we can now configure the different settings, from the top in the graphical user interface:
Global Navigation
Display the same navigation items as the parent site:
$SPPubWeb.Navigation.InheritGlobal = $true
Display the navigation items below the current site:
$SPPubWeb.Navigation.InheritGlobal = $false
Show subsites:
$SPPubWeb.Navigation.GlobalIncludeSubSites = $true
Show pages:
$SPPubWeb.Navigation.GlobalIncludePages = $true
Maximum number of dynamic items to show within this level of navigation:
$SPPubWeb.Navigation.GlobalDynamicChildLimit = 20 (int32)
Current Navigation
Display the same navigation items as the parent site: (both values in combination)
$SPPubWeb.Navigation.InheritCurrent = $true $SPPubWeb.Navigation.ShowSiblings = $false
Display the current site, the navigation items below the current site, and the current site’s siblings: (both values in combination)
$SPPubWeb.Navigation.InheritCurrent = $false $SPPubWeb.Navigation.ShowSiblings = $true
Display only the navigation items below the current site: (both values in combination)
$SPPubWeb.Navigation.InheritCurrent = $false $SPPubWeb.Navigation.ShowSiblings = $false
Show subsites:
$SPPubWeb.Navigation.CurrentIncludeSubSites = $true
Show pages:
$SPPubWeb.Navigation.CurrentIncludePages = $true
Maximum number of dynamic items to show within this level of navigation:
$SPPubWeb.Navigation.CurrentDynamicChildLimit = 20 (int32)
Sorting
Sort automatically:
$SPPubWeb.Navigation.OrderingMethod = "Automatic"
Sort manually:
$SPPubWeb.Navigation.OrderingMethod = "Manual"
Sort sites manually and pages automatically: (This is option is only available with publishing pages)
$SPPubWeb.Navigation.OrderingMethod = "ManualWithAutomaticPageSorting”
When Sort automatically is selected
Sort by Title (shown left):
$SPPubWeb.Navigation.AutomaticSortingMethod = "Title"
Sort by Created Date:
$SPPubWeb.Navigation.AutomaticSortingMethod = "CreatedDate"
Sort by Last Modified Date:
$SPPubWeb.Navigation.AutomaticSortingMethod = "LastModifiedDate"
Sort in ascending order (shown left):
$SPPubWeb.Navigation.SortAscending = $true
Sort in descending order:
$SPPubWeb.Navigation.SortAscending = $false
That’s it, that covers all of the settings I meant to show and this will hopefully be useful to you when scripting the creation and configuration of your sites.
In order to help you get started, you can modify the script included below and that will allow you to configure all of the above settings the same way on all sites in a designated site collection.
Sample script
# get a sitecollection object(SPSite) $SPSite = Get-SPSite -Identity “http://farm.company.local/sitecollection“ # loop through all the subwebs(SPWebs) in the site collection foreach ($SPWeb in $SPSite.AllWebs) { # check so that this is not the root web if (!$SPWeb.IsRootWeb) { # Save AllowUnsafeUpdates setting and set it to allow. $AllowUnsafeUpdatesStatus = SPWeb.AllowUnsafeUpdates SPWeb.AllowUnsafeUpdates = $true # Get a PublishingWeb object for the current web $SPPubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb] ::GetPublishingWeb($SPWeb) # UnComment the setting you will use: # Global settings # $SPPubWeb.Navigation.InheritGlobal = $true # $SPPubWeb.Navigation.InheritGlobal = $false # $SPPubWeb.Navigation.GlobalIncludeSubSites = $true # $SPPubWeb.Navigation.GlobalIncludeSubSites = $false # $SPPubWeb.Navigation.GlobalIncludePages = $true # $SPPubWeb.Navigation.GlobalIncludePages = $false # $SPPubWeb.Navigation.GlobalDynamicChildLimit = 20 # Current settings # See combination of the two below # $SPPubWeb.Navigation.InheritCurrent = $true # $SPPubWeb.Navigation.ShowSiblings = $false # $SPPubWeb.Navigation.CurrentIncludeSubSites = $true # $SPPubWeb.Navigation.CurrentIncludePages = $true # $SPPubWeb.Navigation.CurrentDynamicChildLimit = 20 # Sorting # $SPPubWeb.Navigation.OrderingMethod = "Automatic" # $SPPubWeb.Navigation.OrderingMethod = "Manual" # $SPPubWeb.Navigation.OrderingMethod = "ManualWithAutomaticPageSorting” # $SPPubWeb.Navigation.AutomaticSortingMethod = "Title" # $SPPubWeb.Navigation.AutomaticSortingMethod = "CreatedDate" # $SPPubWeb.Navigation.AutomaticSortingMethod = "LastModifiedDate" # $SPPubWeb.Navigation.SortAscending = $true # $SPPubWeb.Navigation.SortAscending = $false $SPPubWeb.Update() } # cleanup # Set AllowUnsafeUpdates back to original value $SPWeb.AllowUnsafeUpdates = $AllowUnsafeUpdatesStatus $SPWeb.Dispose() } # cleanup $SPSite.Dispose()
Please allow some time for the changes to take affect, it can take a couple of minutes until the changes are reflected in your navgation.
Reference:
PortalNavigation Members
2012-02-16 Updates: Added $SPWeb.AllowUnsafeUpdates section to script.
Hi Thomas – Great article. I’d just run a similar script, specifically for ‘$SPPubWeb.Navigation.GlobalIncludeSubSites = $false’, but ran across an issue. I googled ‘Navigation.GlobalIncludeSubSites’ and found your article, which adds so much more.
In case you can help (I’ve only just started looking at this myself).. I created a script that showed me the current state of ‘$SPPubWeb.Navigation.GlobalIncludeSubSites’, which was ‘true’ for almost all my sites/webs. I ran another script to change that state to $false, did the $SPPubWeb.Update(), but my menus are still showing the subsites. However, when I go to the ‘AreaNavigationSettings.aspx’ for one of these sites, the checkbox for global > show subsites has been unchecked, just not applied. Any ideas why the .Update() isn’t saving the changes when i do it?
Hi kevin.
Thanks for the feedback.
From what I have seen, the setting can sometime take a while to show in the actual navigation. It is a timerjob that updates the navigation, are you sure that the changes were not reflected after a while?
// Thomas
Hi Thomas,
In order to get this to work for me (my environment may be subtly different from yours) I had to amend the script to allow unsafe updates, and update SPWeb rather than SPPubWeb, which I added just before you dispose the SPWeb. i.e.
$SPWeb.AllowUnsafeUpdates = 1;
$SPWeb.Update()
$SPWeb.Dispose()
Hi Kevin, thanks for the update!
Turns out after doing some additional testing, that you are 100% correct Kevin. Thanks a lot for pointing this out. What I apparently had done, was click ok like you described on the top subnodes and it ‘took’.
I will update the post to reflect this.
What you should do though Kevin, is use the following and reset the allowunsafeupdates after you are done:
(Or at least set it back to $false)
$allowUnsafeUpdates = SPweb.AllowUnsafeUpdates
$SPweb.allowUnsafeUpdates = $true;
# Perform the updates...
$SPweb.allowUnsafeUpdates = $allowUnsafeUpdates;
Thanks also to ‘admin’ for that tip:
http://solutionarchitects.org/articles/2011/02/24/better-way-to-do-spweb-allowunsafeupdates.htm
// Thomas
Thomas
In few words; You’ve made my day.
Great, clear and good article
Eric
Hi Eric.
Feedback like this is the reason why I keep on…
Thanks
This looks like a great script but I get a few errors.
I’m working on figuring out if this could be because I’m using a remote session of poweshell.
These errors mysteriously vanished when I ran the script today, which is quite odd since I even tried running them in a new session last time.
Hi.
Great that it worked…could be anything that caused it.
Regards
The script does make the desired changes, but when the changes are not visible in UI for Current Navigation. I am trying to configure Display the current site, the navigation items below the current site, and the current site’s siblings for Current Navigation.
Hi Kumar.
Depending on the version of SharePoint, I remember that you have to check show subsites on the child site to make it visible on the parent…
Could that be it?
Regards
// Thomas