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.
1: Add-PSSnapin "Microsoft.SharePoint.PowerShell"
2:
3: function Get-TermSetsCSV() {
4: param($SiteUrl, $CSVOutput)
5:
6: $empty = ""
7:
8: $taxonomySite = Get-SPSite -Identity $SiteUrl
9:
10: #Connect to Term Store in the Managed Metadata Service Application
11: $taxonomySession = Get-SPTaxonomySession -site $taxonomySite
12: $taxonomyTermStore = $taxonomySession.TermStores | Select Name
13: $termStore = $taxonomySession.TermStores[$taxonomyTermStore.Name]
14:
15: foreach ($group in $termStore.Groups)
16: {
17: foreach($termSet in $group.TermSets)
18: {
19: $terms = @()
20:
21: #The path and file name, in this case I did C:\TermSet\TermSetName.csv
22: $CSVFile = $CSVOutput + '\' + $termSet.Name + '.csv'
23:
24: #From TechNet: The first line of the file must contain 12 items separated by commas
25: $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
26: $terms+=$firstLine
27:
28: #Now we start to add a line in the file for each term in the term set
29: foreach ($term in $termSet.GetAllTerms())
30: {
31:
32: $tempTerm = $term
33: $counter = 0
34: $tempTerms = @("","","","","","","")
35:
36: #this while loop makes sure you are using the root term then counts how many child terms there are
37: while (!$tempTerm.IsRoot)
38: {
39: $tempTerm = $tempTerm.Parent
40: $counter = $counter + 1
41: }
42:
43: $start = $counter
44:
45: #this makes sure that any columns that would need to be empty are empty
46: #i.e. if the current term is 3 levels deep, then the 4th, 5th, and 6th level will be empty
47: while ($counter -le 6)
48: {
49: $tempTerms[$counter] = $empty
50: $counter = $counter + 1
51: }
52:
53: #start with the current term
54: $tempTerm = $term
55:
56: #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)
57: while ($start -ge 0)
58: {
59: $tempTerms[$start] = $tempTerm.Name
60: $tempTerm = $tempTerm.Parent
61: $start = $start - 1
62: }
63:
64: #create a new line in the CSV file
65: $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]
66:
67: #add the new line
68: $terms+=$CSVLine
69: }
70:
71: #export all of the terms to a CSV file
72: $terms | Export-Csv $CSVFile -notype
73: }
74: }
75: $taxonomySite.dispose()
76: }
77:
78: #constructor
79: function New-TermLine() {
80: param($TermSetName, $TermSetDescription, $LCID, $AvailableForTagging, $TermDescription, $Level1, $Level2, $Level3, $Level4, $Level5, $Level6, $Level7)
81:
82: $term = New-Object PSObject
83:
84: $term | Add-Member -Name "TermSetName" -MemberType NoteProperty -Value $TermSetName
85: $term | Add-Member -Name "TermSetDescription" -MemberType NoteProperty -Value $TermSetDescription
86: $term | Add-Member -Name "LCID" -MemberType NoteProperty -Value $LCID
87: $term | Add-Member -Name "AvailableForTagging" -MemberType NoteProperty -Value $AvailableForTagging
88: $term | Add-Member -Name "TermDescription" -MemberType NoteProperty -Value $TermDescription
89: $term | Add-Member -Name "Level1" -MemberType NoteProperty -Value $Level1
90: $term | Add-Member -Name "Level2" -MemberType NoteProperty -Value $Level2
91: $term | Add-Member -Name "Level3" -MemberType NoteProperty -Value $Level3
92: $term | Add-Member -Name "Level4" -MemberType NoteProperty -Value $Level4
93: $term | Add-Member -Name "Level5" -MemberType NoteProperty -Value $Level5
94: $term | Add-Member -Name "Level6" -MemberType NoteProperty -Value $Level6
95: $term | Add-Member -Name "Level7" -MemberType NoteProperty -Value $Level7
96:
97: return $term
98: }
99:
100: Get-TermSetsCSV -SiteUrl "Your Site URL" -CSVOutput "C:\TermSet"