Home > Uncategorized > SharePoint 2010: Export TermSet to .csv

SharePoint 2010: Export TermSet to .csv

I recently had to export all my TermSets  in my SharePoint 2010 environment and move the TermSets over to a new enviornment in a different domain.  I wanted to do this instead of having to hand jam in every single termset over again, especially if you have hundreds of terms.  Now there are many ways to export TermSets in SharePoint 2010, however there is only one correct way to to this in the correct format to successfully import it back into a new environment.

Thanks to https://gallery.technet.microsoft.com/office/PowerShell-for-SharePoint-a838b5d0/view/Reviews they already did the hard work for us.  I just basically took their script and modified it to work in my environment.

Below is how I leveraged their script to work for me.

——————————————
$outputDir = “c:\temp”

function Get-TermSetsCSV() {
param($CSVOutput)

$empty = “”

$termSet = Get-SPTaxonomySession -Site http://portal
$termStore = $termSet.TermStores[0]
$termGroup = $termStore.Groups[“NAME OF TERM GROUP“]
$termStore = $termGroup.Name

foreach ($termSet in $termGroup.TermSets)
{
$terms = @()

#The path and file name, in this case I did c:\temp\TermSetName.csv
$CSVFile = $CSVOutput + ‘\’ + $termSet.Name + ‘.csv’

#From TechNet:  The first line of the file must contain 12 items separated  by commas
$firstLine = New-TermLine -TermSetName $termSet.Name -TermSetDescription $empy -LCID $empy -AvailableForTagging “TRUE” -TermDescription $empty -Level1 $empty -Level2 $empty -Level3 $empty -Level4 $empty -Level5 $empty -Level6 $empty -Level7 $empty
$terms+=$firstLine

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 curren 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 the terms to a CSV File
$terms | Export-Csv $CSVFile -notype
}
}
#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 -CSVOutput $outputDir

 

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: