• images
  • 2:34 am
  • images
  • 4 Comments.

Powershell to create and reuse Managed Navigation for multiple sites in SharePoint 2013

As promised earlier, below is a powershell script to create managed navigation in SharePoint 2013 using powershell. Before reading below, I would strongly suggest to read my previous post on managed navigation here

Add-PSSnapin Microsoft.SharePoint.Powershell

#change these variables based on environment
$siteName = "Site url" 
$siteSearchName = "Search site url" 
$termStoreName = “Managed Metadata Service”
$termGroupName = “My Navigation”
$termsetName = "MyNavigationTermset"
$termsetSearchName = "MyNavigationTermset Search"
$appendUrl = "Site url"

function CreateTerm( $parent, $name, $url )  
 {  
  Write-Host "Adding term $name to parent $parent.Name"  
   $term = $parent.CreateTerm("$name", 1033)  
  $term.IsAvailableForTagging = $true    
  $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 = $appendUrl + $url
  }
  $term.SetLocalCustomProperty("_Sys_Nav_SimpleLinkUrl", $newUrl)   
  return $term  
 } 

# Create termgroup and termset for My Site

$site = Get-SPSite -Identity $siteName
$session = Get-SPTaxonomySession -Site $siteName
$termStore = $session.TermStores[$termStoreName]
$group = $termstore.CreateGroup($termGroupName)
$group.Description = "Term Group description"
$termStore.CommitAll()

Write-Host "TermGroup - $termGroupName created successfully"  

$termsetName = "Name of your navigation termset"
$termSet = $group.CreateTermSet($termsetName,1033)
$termSet.Description = “Navigation TermSet description”
$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 for My site

$wwrTerm = CreateTerm $termSet “WHO WE ARE” "/who"
$wavTerm = CreateTerm $termSet “WORKING AT OUR COMPANY” "/working"
$wynTerm = CreateTerm $termSet “ABOUT US” "/what"

$termStore.CommitAll()

$termSet.CustomSortOrder = $wwrTerm.id.ToString()+":"+$wavTerm.id.ToString()+":"+$wynTerm.id.ToString()

# create sub terms for My site

$term1 = CreateTerm $wwrTerm "Company" "/who/company"
$term2 = CreateTerm $wwrTerm "Organization" "/who/organization"
$term3 = CreateTerm $wwrTerm "Products" "/who/products"

$term4 = CreateTerm $wavTerm "Perks" "/working/perks"
$term5 = CreateTerm $wavTerm "Recognition" "/working/recognition"
$term6 = CreateTerm $wavTerm "HR Info" "/who/organization/hr"

$term7 = CreateTerm $wynTerm "Resources and Services" "/what/resources/"
$term8 = CreateTerm $wynTerm "Policies" "/what/policies"
$term9 = CreateTerm $wynTerm "Contact Us" "/what/contact"

$wwrTerm.CustomSortOrder = $term1.id.ToString() +":"+ $term2.id.ToString() +":"+ $term3.id.ToString()
$wavTerm.CustomSortOrder = $term4.id.ToString() +":"+ $term5.id.ToString() +":"+ $term6.id.ToString()
$wynTerm.CustomSortOrder = $term7.id.ToString() +":"+ $term8.id.ToString() +":"+ $term9.id.ToString()
$termStore.CommitAll()

Write-Host "Terms for TermSet - $termsetName created successfully"  

# create terms for My Site Search

$termSet = $group.CreateTermSet($termsetSearchName,1033)
$termSet.Description = “Navigation TermSet for Search site”
$termSet.IsAvailableForTagging = $false
$termSet.IsOpenForTermCreation = $false
$navigationSet = $group.TermSets[$termsetName]
$navigationSet.SetCustomProperty("_Sys_Nav_IsNavigationTermSet", "True")
$termStore.CommitAll()

Write-Host "TermSet - $termsetSearchName created successfully"  

$wwrTermSearch = $termSet.ReuseTerm($wwrTerm,$true);
$wavTermSearch = $termSet.ReuseTerm($wavTerm,$true);
$wynTermSearch = $termSet.ReuseTerm($wynTerm,$true);

$termSet.CustomSortOrder = $wwrTerm.id.ToString()+":"+$wavTerm.id.ToString()+":"+$wynTerm.id.ToString()

$termStore.CommitAll()

$wwrTermSearch.CustomSortOrder = $term1.id.ToString() +":"+ $term2.id.ToString() +":"+ $term3.id.ToString()
$wavTermSearch.CustomSortOrder = $term4.id.ToString() +":"+ $term5.id.ToString() +":"+ $term6.id.ToString()
$wynTermSearch.CustomSortOrder = $term7.id.ToString() +":"+ $term8.id.ToString() +":"+ $term9.id.ToString()
$termStore.CommitAll()

Write-Host "Terms for TermSet - $termsetName created successfully"

The script above creates a navigation termset with some dummy terms for “My Site” and reuses the same terms for “My Site Search” so that both the sites can have the same managed navigation. This script also takes care of custom sort order for parent and child level terms. Also, note that there is a concept of “append url” which helps in creating the full url before assigning it to the term.

4 Comments

Joe

December 17, 2013 2:17 pm Reply

Is there anyway to add a term dynamically under a term? In a sense nested terms?

    admin

    December 21, 2013 9:52 pm Reply

    Yes, you can add terms below another term. Termstore allows 6 levels of term hierarchy.

      Yves

      January 21, 2014 6:53 pm Reply

      Can you explain how you would add a term under a term using powershell command?

Leave a Reply