maandag 11 maart 2013

Export Termset and Terms





Here is a script to export the termsets and terms from SharePoint.

I found this script on this site on internet.


function Export-SPTermSet {
  param (
  [Microsoft.SharePoint.Taxonomy.TermCollection]$terms,
  [int]$level = 1,
  [string]$previousTerms = ""
)

if ($level -ge 1 -or $level -le 7) 
{
   if ($terms.Count -gt 0 ) 
   {
      $termSetName = ""
      if ($level -eq 1) 
      {
         $termSetName =  """" + $terms[0].TermSet.Name.Replace([System.Text.Encoding]::UTF8.GetString($amp), "&") + """"
      }
      $terms | ForEach-Object {
      $currentTerms = $previousTerms + ",""" + $_.Name.Replace([System.Text.Encoding]::UTF8.GetString($amp), "&") + """";

      $file.Writeline($termSetName + ",""" + $_.TermSet.Description + """" + ",," + $_.IsAvailableForTagging +"," + $_.GetDescription() + $currentTerms);

      if ($level -lt 7) 
      {
         Export-SPTermSet $_.Terms ($level + 1) ($previousTerms + $currentTerms)
      }
   }
}
}
}


function Export-SPTerms {
param (
[string]$siteUrl = $(Read-Host -prompt "Please provide the site collection URL"),
[string]$termGroupName = $(Read-Host -prompt "Please provide the term group name to export"),
[string]$saveLocation = $(Read-Host -prompt "Please provide the path of the folder to save the CSV file to")
)

if ([IO.Directory]::Exists($saveLocation) -eq $false)
{
New-Item ($saveLocation) -Type Directory | Out-Null
}

$taxonomySession = Get-SPTaxonomySession -site $siteUrl
$taxonomyTermStore =  $taxonomySession.TermStores | Select Name
$termStore = $taxonomySession.TermStores[$taxonomyTermStore.Name]

# Ampersands are stored as fullwidth ampersands (see http://www.fileformat.info/info/unicode/char/ff06/index.htm)
[Byte[]] $amp = 0xEF,0xBC,0x86

foreach ($group in $termStore.Groups) {
if ($group.Name -eq $termGroupName) {
foreach ($termSet in $group.TermSets) {
# Remove unsafe file system characters from filename
$parsedFilename =  [regex]::replace($termSet.Name, "[^a-zA-Z0-9\\-]", "_")
$file = New-Object System.IO.StreamWriter($saveLocation + "\termset_" + $parsedFilename + ".csv")

# Write out the headers
$file.Writeline("Term Set Name,Term Set Description,LCID,Available for Tagging,Term Description,Level 1 Term, Level 2 Term,Level 3 Term,Level 4 Term,Level 5 Term,Level 6 Term,Level 7 Term")

try {
Export-SPTermSet $termSet.Terms
}
finally {
$file.Flush()
$file.Close()
}
}
}
}
}