Thursday, August 15, 2019

Example Powershell to Automatically Update SharePoint Managed Navigation in Team Sites “associated” to a central Publishing Site


This set of Powershell commands goes along with a demonstration I do at various SharePoint conferences during my presentation titled “Making the Most of SharePoint Managed Metadata”.
In the demo, I show how you can achieve something similar to the global navigation experience that SharePoint Online now offers using Hub sites and associated Team Sites (see this blog post for what that is all about), but by using the traditional SharePoint Publishing Site template along with traditional Team Site templates.  This can be done in SharePoint Server 2010, 2013, 2016, 2019 and SharePoint Online.

In my demo, I show how to create a new Team Site and then “associate” it with the main Publishing site by inheriting (actually, the official terminology is “Pin Term with Children”) its Managed Metadata-based Global Navigation.

The only catch with this is that after the new Team Site is created and its Global Navigation “associated” with the central Publishing site, the Global Navigation in any existing Team Sites does not get automatically updated.  So, in my demo I run some SharePoint Powershell as the last step that will loop through all of the “associated” Team Sites and update their Managed Navigation settings to include the new Team Site.

This Powershell is very specific to my demonstration and only provided here as a resource for someone wanting to do something similar.  To use it, you would need to read through it and make an effort to think about what it is doing.  Then, you would need to figure out how you could adapt it to work in your specific scenario and\or environment.

Also, this Powershell will only work with on-premises SharePoint Server.  If you need for it to work with SharePoint Online, you would have to take the concepts and come up with your own SharePoint Online version.

Example from my “Making the Most of SharePoint Managed Metadata” demo:

       

Add-PSSnapin "Microsoft.SharePoint.PowerShell" –ErrorAction SilentlyContinue

#Set variables

#The name of the Term Store in the local SharePoint Farm
$termStoreName = "Managed Metadata Service"

#The name of the Global Term Group that is used in the central Publishing Site
$globalTermGroupName = "Navigation"
#The name of the Global Term Set that is used in the Global Term Group
$globalTermSetName = "Intranet Global Navigation"

#Get the list of site collections that are attached to the central Publishing site collection
#In this demo, I have a list named "IntranetAttachedSites" that has an item for each Team site collection that is to be "associated"
$requestPortalWeb = Get-SPWeb http://intranet/sites/requests
$list = $requestPortalWeb.Lists["IntranetAttachedSites"]
$listItems = $list.GetItems()


#Update the navigation for each attached site collection
foreach ($listItem in $listItems) 
{
    $localTermGroupName = $listItem["Term Group Name"]
    $localTermSetName = $listItem["Term Set Name"]

    $web= Get-SPWeb $listItem["Title"]
    $site = $web.Site
    $navSettings = New-Object Microsoft.SharePoint.Publishing.Navigation.WebNavigationSettings($web)
    $taxSession = Get-SPTaxonomySession -Site $site
    $termStore = $taxSession.TermStores[$termStoreName]

    $globalTermGroup = $termStore.Groups[$globalTermGroupName]
    $globalTermSet = $globalTermGroup.TermSets[$globalTermSetName]

    $localTermGroup = $termStore.Groups[$localTermGroupName]
    $localTermSet = $localTermGroup.TermSets[$localTermSetName]

    # remove all the existing terms from the local Term Set
    $localTermSet.Terms|ForEach-Object{$_.delete()}
    $termStore.CommitAll()

    # pin the terms to the local term set from the master term set
    $globalTermSet.Terms|ForEach-Object{
       $t = $localTermSet.ReuseTermWithPinning($_)
    }

    # copy the sort order from the master term set
    $localTermSet.CustomSortOrder = $globalTermSet.CustomSortOrder 
    $termStore.CommitAll()
}

       
 

No comments: