PowerShell to add and sort Managed Metadata navigation – on premises

Here's a useful snippet of PowerShell code for creating Managed Metadata navigation and sorting it – on premises

Add-PSSnapin Microsoft.SharePoint.Powershell

# Change these variables based on environment ****
$centralAdminURL = "http://centraladmin"
$siteCollectionURL = "http://sitecollection"

$termStoreName = “Managed Metadata Service”
$termGroupName = “Navigation Group”
$termGroupDescription = "Group Description"
$termsetName = "Navigation Termset"
$termSetDescription = "Termset Description"
# *************************************************


function CreateTerm($parent, $name, $url)  
 {  
    Write-Host "Adding term $name to parent $parent.Name"  
    $term = $parent.CreateTerm("$name", 1033)  
    $term.IsAvailableForTagging = $false    
    $newUrl = $url
    if(!$url.ToLower().Contains("http"))
    {  
        # Logic to append site url to relative path to create complete url which is needed for Simple Link Navigation
        $newUrl = $url
    }

    $term.SetLocalCustomProperty("_Sys_Nav_SimpleLinkUrl", $newUrl)   
    return $term  
 } 

$site = Get-SPSite -Identity $centralAdminURL
$session = Get-SPTaxonomySession -Site $centralAdminURL
$termStore = $session.TermStores[$termStoreName]

# Remove if exists already
$group = $termStore.Groups[$termGroupName]
if ($group -ne $null)
{
    Write-Host "Removing terms"
    $group.TermSets|foreach{
        $_.Delete()
    }
    $group.Delete()
    $termstore.CommitAll()
}

# Create term group and term set
$group = $termstore.CreateGroup($termGroupName)
$group.Description = $termGroupDescription
$termStore.CommitAll()

Write-Host "TermGroup - $termGroupName created successfully"  

$termSet = $group.CreateTermSet($termsetName,1033)
$termSet.Description = $termSetDescription
$termSet.IsAvailableForTagging = $false
$termSet.IsOpenForTermCreation = $false
$navigationSet = $group.TermSets[$termsetName]
$navigationSet.SetCustomProperty("_Sys_Nav_IsNavigationTermSet", "True")
$termStore.CommitAll()

Write-Host "TermSet - $termsetName created successfully"  

# Create terms

# Home
$homeTerm = CreateTerm $termSet "Home" $($siteCollection + "/")

# Other term set

$otherTerm = CreateTerm $termSet "Other Term" $($siteCollection + "/subsite/Pages/home.aspx")

$termStore.CommitAll()

# Create sub terms
$otherTermSub1 = CreateTerm $otherTerm "Other Term Sub 1" $($siteCollection + "/subsite/Pages/othertermsub1.aspx")
$otherTermSub2 = CreateTerm $otherTerm "Other Term Sub 2" $($siteCollection + "/subsite/Pages/othertermsub2.aspx")
$otherTermSub3 = CreateTerm $otherTerm "Other Term Sub 3" $($siteCollection + "/subsite/Pages/othertermsub3.aspx")
$otherTermSub4 = CreateTerm $otherTerm "Other Term Sub 4" $($siteCollection + "/subsite/Pages/othertermsub4.aspx")

# Set sort order
$customSortOrder = $otherTermSub1.id.ToString() + ":" + $otherTermSub2.id.ToString() + ":" + $otherTermSub3.id.ToString() + ":" + $otherTermSub4.id.ToString()
$otherTerm.CustomSortOrder = $customSortOrder

$termStore.CommitAll()

Write-Host "Terms for Term Set - $termsetName created successfully"   

 

Leave a Reply

Your email address will not be published. Required fields are marked *