Monday, 16 September 2013

SharePoint 2013: Add Managed Metatadata navigation nodes in Powershell

The following script will add some managed metadata for navigation purposes. It will create a term store call 'My Group' and a term set  called  'My Term Set'. It will then add navigation links to Google and Hotmail.

# define some variables
$defaultMetadataServiceProxy = "Managed Metadata Service"
$groupName = "My Group"
$termSetName="My Term Set Name

#create the function to add the terms
function CreateNavigationTerm($termSet, $termName, $link, $termstore)
{
Write-Host "Add $termName"
$term = $termset.CreateTerm("$termName",1033)
$term.SetLocalCustomProperty("_Sys_Nav_SimpleLinkUrl", $link)
$termstore.CommitAll()
Write-Host "Successfully added $termName"
}

#get central admin
$adminwebapp = Get-SPWebapplication -includecentraladministration | where {$_.IsAdministrationWebApplication}

$caSite=Get-SPSite $adminwebapp.Url

#get the Taxonomy session
$session = new-object Microsoft.SharePoint.Taxonomy.TaxonomySession($caSite)

#get the Term Store
$termstore = $session.TermStores | where {$_.Name -eq $defaultMetadataServiceProxy}

#create the group (if required)
$rootgroup = $termstore.Groups["$groupName"]
if ($rootgroup -eq $null)
{
$rootgroup = = $termstore.CreateGroup("$groupName");
}

#delete the term set (if it exists)
if ($rootgroup.TermSets["$termSetName"] -ne $null)
{
$rootgroup.TermSets["$termSetName"].Delete();
}

# create it anew
$termset = $rootgroup.CreateTermSet("$termSetName",1033)
$termset.SetCustomProperty("_Sys_Nav_IsNavigationTermSet", "True")

# add the navigation terms
CreateNavigationTerm $termset "Google" "http://www.google.com" $termstore
CreateNavigationTerm $termset "Hotmail" "http://www.hotmail.com" $termstore

Write-Host "Done!!"



No comments:

Post a comment