Monday, May 23, 2011

Create a new Managed Metadata term with synonym in SharePoint 2010 using PowerShell

Use this script if you want to create a new term in the SharePoint 2010 Managed Metadata service with PowerShell. Incidentally, the Managed Metadata CSV import format provided out-of-the-box in SharePoint 2010 does not support synonyms, so you could easily use this code along with the Import-Csv PowerShell cmdlet to come up with a replacement format if you needed to support synonyms (will get around to posting a script at some point):

#Connect to Central Admin
$taxonomySite = get-SPSite http://centraladminsite:port

#Connect to Term Store in the Managed Metadata Service Application
$taxonomySession = Get-SPTaxonomySession -site $taxonomySite
$termStore = $taxonomySession.TermStores["Managed Metadata Service"]
write-host "Connection made with term store -"$termStore.Name

#Connect to the Group and Term Set
$termStoreGroup = $termStore.Groups["Group Name"]
$termSet = $termStoreGroup.TermSets["Term Set Name"]

#Create term, term description, and a synonym
$term = $termSet.CreateTerm("Test Term", 1033)
$term.SetDescription("This is a test", 1033)
$term.CreateLabel("This is a test synonym", 1033, $false)

#Update the Term Store
$termStore.CommitAll()

#Dispose of taxonomy site object
$taxonomySite.Dispose()

No comments: