Reliably Deploying SharePoint 2010 Taxonomies

One a recent project for Slalom Consulting, my employer, we had a project that made use of SharePoint 2010 Managed metadata including groups, term sets and terms. As we began to create new environments, one of the laborious tasks was to populate the Taxonomy Term Store using the term store manager page accessible via Central Administration or the related site collection (“_layouts/termstoremanager.aspx”). The terms are loaded into the Taxonomy Term Store via CSV file that adheres to a specific format that you can see via the url of your SharePoint Central Administration site: http://centraladminurl/_layouts/1033/ImportTermSet.csv

To manually import a file, you must first create group, then click on that group and chose “Import Term Set”:

image

You are then presented with a dialog box where you can select a CSV file that adheres to the required format to import:

image

This process is fine if you only have one or two terms to load but as your continue to build out a solution and have to do this process over and over again, it can be come tedious. The following is my PowerShell script that loops through all the .csv files in the same directory as the PowerShell script. The caution is the script deletes the terms each time, so any site columns you create from this will need to adjusted if you delete and recreate the term set. However, this is useful during setting up environments during the development process as you create site collections. Use with caution Smile.  I am also very much open to feedback on the PowerShell script and how to improve it.

try

{

 if(((Get-PSSnapin | foreach {$_.Name}) -contains "Microsoft.SharePoint.PowerShell") -eq $false)

 {

 Add-PSSnapIn Microsoft.SharePoint.PowerShell -ErrorAction Stop

 }

}

catch

{

 Write-Host -ForegroundColor "red" "This script only works on machines with SharePoint 2010 installed.`n"

 Exit

}

 

function Pause ($Message="Press any key to continue...")

 {

 # The ReadKey functionality is only supported at the console (not is the ISE)

 if (!$psISE)

 {

 Write-Host -NoNewLine $Message

 $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

 Write-Host ""

 }

 }

 

function AddCSVToTermSet([string] $termGroup, [string] $termSetCSVPath)

{

$termStore = $session.TermStores[$mmdServiceName]

if ($termStore.Groups[$termGroup] -eq $null) {

    $group = $termstore.CreateGroup($termGroup);

    $termStore.CommitAll() 

}

else {

    $group = $termStore.Groups[$termGroup]

}

# Import Taxonomy 

# Create an instance of ImportManager

$Importer = $termStore.GetImportManager()

$varImported = ""

$varMessages = ""

$reader = [System.IO.File]::OpenText($termSetCSVPath) 

 

# Syntax: ImportTermSet(TermGroup, TextReader, out isImported, out errorMessage)

$Importer.ImportTermSet($group ,$reader, ([REF] $varImported), ([REF] $varMessages))

}

 

$termGroup = "Location"

$centralAdminUrl = $(Get-spwebapplication -includecentraladministration | where {$_.IsAdministrationWebApplication} | Select-Object -Property Url).Url

$session = Get-SPTaxonomySession -site $centralAdminUrl

$mmdServiceName = $(((Get-SPFarm).ServiceProxies | Where {$_.TypeName -eq "Managed Metadata Web Service Proxy"}).ApplicationProxies | Select-Object -Property DisplayName).DisplayName

 

#delete all previous, 

#use with caution as any site solumns created with this will lose their relationship if the terms are deleted

 

$termStore = $session.TermStores[$mmdServiceName]

$termStore.Groups[$termGroup].TermSets | ForEach-Object {$_.Delete() }

$termStore.Groups[$termGroup].Delete()

$termStore.CommitAll() 

 

#process all CSV in same directory

$files = Get-ChildItem "." | Where {$_.extension -eq ".csv"}

Foreach ($csv in $files) {

    AddCSVToTermSet $termGroup $csv.FullName

}

 

Pause