This post originally appeared on the Perficient blog
I don’t know about you, but I really wish there was a way to export managed metadata in SharePoint, especially since SharePoint teases you with the ‘Import Term Set’ option. To get around this I’ve written a little script to export all of the term sets in a term store (easily customizable to just export the term sets in a particular group or just 1 term set if you choose to go that route) to a CSV file that follows the Microsoft formatting convention needed to use the Import Term Set option. It’s not perfect, but it definitely gets the job done. I thought I’d share it with you all to hopefully make someone else’s life a little easier. For information on the formatting of the CSV file for import please see this Technet article.
Code (Powershell)
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
function Get-TermSetsCSV() {
param($SiteUrl, $CSVOutput)
$empty = ""
$taxonomySite = Get-SPSite -Identity $SiteUrl
#Connect to Term Store in the Managed Metadata Service Application
$taxonomySession = Get-SPTaxonomySession -site $taxonomySite
$taxonomyTermStore = $taxonomySession.TermStores | Select Name
$termStore = $taxonomySession.TermStores[$taxonomyTermStore.Name]
foreach ($group in $termStore.Groups)
{
foreach($termSet in $group.TermSets)
{
$terms = @()
#The path and file name, in this case I d\TermSet\TermSetName.csv
$CSVFile = $CSVOutput + '\' + $termSet.Name + '.csv'
#From Tec The first line of the file must contain 12 items separated by commas
$firstLine = New-TermLine -TermSetName $termSet.Name -TermSetDescription $empty -LCID $empty -AvailableForTagging "TRUE" -TermDescription $empty -Level1 $empty -Level2 $empty -Level3 $empty -Level4 $empty -Level5 $empty -Level6 $empty -Level7 $empty
$terms+=$firstLine
#Now we start to add a line in the file for each term in the term set
foreach ($term in $termSet.GetAllTerms())
{
$tempTerm = $term
$counter = 0
$tempTerms = @("","","","","","","")
#this while loop makes sure you are using the root term then counts how many child terms there are
while (!$tempTerm.IsRoot)
{
$tempTerm = $tempTerm.Parent
$counter = $counter + 1
}
$start = $counter
#this makes sure that any columns that would need to be empty are empty
#i.e. if the current term is 3 levels deep, then the 4th, 5th, and 6th level will be empty
while ($counter -le 6)
{
$tempTerms[$counter] = $empty
$counter = $counter + 1
}
#start with the current term
$tempTerm = $term
#fill in the parent terms of the current term (there should never be children of the current term--the child term will have its own line in the CSV)
while ($start -ge 0)
{
$tempTerms[$start] = $tempTerm.Name
$tempTerm = $tempTerm.Parent
$start = $start - 1
}
#create a new line in the CSV file
$CSVLine = New-TermLine -TermSetName $empty -TermSetDescription $empty -LCID $empty -AvailableForTagging "TRUE" -TermDescription $empty -Level1 $tempTerms[0] -Level2 $tempTerms[1] -Level3 $tempTerms[2] -Level4 $tempTerms[3] -Level5 $tempTerms[4] -Level6 $tempTerms[5] -Level7 $tempTerms[6]
#add the new line
$terms+=$CSVLine
}
#export all of the terms to a CSV file
$terms | Export-Csv $CSVFile -notype
}
}
$taxonomySite.dispose()
}
#constructor
function New-TermLine() {
param($TermSetName, $TermSetDescription, $LCID, $AvailableForTagging, $TermDescription, $Level1, $Level2, $Level3, $Level4, $Level5, $Level6, $Level7)
$term = New-Object PSObject
$term | Add-Member -Name "TermSetName" -MemberType NoteProperty -Value $TermSetName
$term | Add-Member -Name "TermSetDescription" -MemberType NoteProperty -Value $TermSetDescription
$term | Add-Member -Name "LCID" -MemberType NoteProperty -Value $LCID
$term | Add-Member -Name "AvailableForTagging" -MemberType NoteProperty -Value $AvailableForTagging
$term | Add-Member -Name "TermDescription" -MemberType NoteProperty -Value $TermDescription
$term | Add-Member -Name "Level1" -MemberType NoteProperty -Value $Level1
$term | Add-Member -Name "Level2" -MemberType NoteProperty -Value $Level2
$term | Add-Member -Name "Level3" -MemberType NoteProperty -Value $Level3
$term | Add-Member -Name "Level4" -MemberType NoteProperty -Value $Level4
$term | Add-Member -Name "Level5" -MemberType NoteProperty -Value $Level5
$term | Add-Member -Name "Level6" -MemberType NoteProperty -Value $Level6
$term | Add-Member -Name "Level7" -MemberType NoteProperty -Value $Level7
return $term
}
Get-TermSetsCSV -SiteUrl "Your Site URL" -CSVOutput "C:\TermSet"