Select Taxonomy TermSet for site collection navigation using PowerShell
-
02-01-2021 - |
Question
Can someone help me to figure out how to force a certain site collection to start using a term set for navigation?
I already have a prepared term set that can be used for navigation, but I don't want to use GUI to apply it. I am in the process of automating this step. This is a sample code that I've got so far.
Add-PSSnapin microsoft.sharepoint.powershell
function Get-TaxonomySessionDefault()
{
$centralAdmin = Get-SPWebApplication -IncludeCentralAdministration | Where {$_.IsAdministrationWebApplication} | Get-SPSite
$session = new-object Microsoft.SharePoint.Taxonomy.TaxonomySession($centralAdmin)
return $session
}
function Get-TermStoreDefault()
{
$session = Get-TaxonomySessionDefault
$serviceApp = Get-SPServiceApplication | Where {$_.TypeName -like "*Metadata*"}
$termStore = $session.TermStores[$serviceApp.Name]
return $termStore;
}
$termStore = Get-TermStoreDefault
$termGroup = $termStore.Groups["Site Collection - siteUrl.ca-sites-subsite"]
$termSet = $termGroup.TermSets["Subsite Navigation"]
$site = get-spsite "https://siteUrl.ca/sites/subsites"
# How Do I apply $termSet to become $site's navigation?
# Please, note, that $termSet is ready to be used as a navigation term set
Solution
Ok, I think, I've got it working:
Add-PSSnapin microsoft.sharepoint.powershell
function Get-TaxonomySessionDefault()
{
$centralAdmin = Get-SPWebApplication -IncludeCentralAdministration | Where {$_.IsAdministrationWebApplication} | Get-SPSite
$session = new-object Microsoft.SharePoint.Taxonomy.TaxonomySession($centralAdmin)
return $session
}
function Get-TermStoreDefault()
{
$session = Get-TaxonomySessionDefault
$serviceApp = Get-SPServiceApplication | Where {$_.TypeName -like "*Metadata*"}
$termStore = $session.TermStores[$serviceApp.Name]
return $termStore;
}
$termStore = Get-TermStoreDefault
$termGroup = $termStore.Groups["Site Collection - siteUrl.ca-sites-subsite"]
$termSet = $termGroup.TermSets["Subsite Navigation"]
$site = get-spsite "https://siteUrl.ca/sites/subsites"
$web = $site.RootWeb
$navigation = $web.Navigation
$pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
$webNavigationSettings = new-object Microsoft.SharePoint.Publishing.Navigation.WebNavigationSettings -ArgumentList @($web)
$webNavigationSettings.GlobalNavigation.Source = [Microsoft.SharePoint.Publishing.Navigation.StandardNavigationSource]::TaxonomyProvider
$webNavigationSettings.GlobalNavigation.TermStoreId = $termStore.Id;
$webNavigationSettings.GlobalNavigation.TermSetId = $termSet.Id;
$webNavigationSettings.Update()
$pubWeb.Update()
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange