The Managed Metadata service provides the “Import Term Set” Out of the Box.
Unfortunately it does not provide the same “Export Term Set” capability. I had a requirement to export a term set to be reviewed by the business before importing the term set to another SharePoint farm.
The Sample Import File has the columns “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” and “Level 7 Term”.
I have written the PowerShell script below using CSOM to export a termset into a csv file with the same columns as in the sample import file.
In the script, update the variables $User and $Site. When the script is run you will be prompted to enter the term store group and term set.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Set-ExecutionPolicy -Scope CurrentUser Unrestricted | |
#Specify admin user and SharePoint site URL | |
##update login | |
$User = "domain\username" | |
##update siteUrl | |
$Site = "http://portal" | |
#Adding references to SharePoint client assemblies | |
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll" | |
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll" | |
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll" | |
$Pwd = Read-Host -Prompt "Enter your password" -AsSecureString | |
$GroupName = Read-Host -Prompt "Enter the metadata group name" | |
$TermSetName = Read-Host -Prompt "Enter the termset name" | |
#Recursive function to get terms | |
function GetTerms([Microsoft.SharePoint.Client.Taxonomy.Term] $Term,[String]$ParentTerm,[int] $Level) | |
{ | |
$Terms = $Term.Terms; | |
$Context.Load($Terms) | |
$Context.ExecuteQuery(); | |
if($ParentTerm) | |
{ | |
$ParentTerm = $ParentTerm + "," + $Term.Name; | |
} | |
else | |
{ | |
$ParentTerm = $Term.Name; | |
} | |
Foreach ($SubTerm in $Terms) | |
{ | |
$Level = $Level + 1; | |
#up to 7 terms levels are written | |
$NumofCommas = 7 – $Level; | |
$commas =""; | |
For ($i=0; $i -lt $NumofCommas; $i++) | |
{ | |
$commas = $commas + ","; | |
} | |
$file.Writeline("," + "," + "," + "," + $Term.Description + "," + $ParentTerm + "," + $SubTerm.Name + $commas ); | |
GetTerms -Term $SubTerm -ParentTerm $ParentTerm -Level $Level; | |
} | |
} | |
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($Site) | |
$Credentials = New-Object System.Net.NetworkCredential($User,$Pwd); | |
$Context.Credentials = $Credentials | |
$MMS = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($Context) | |
$Context.Load($MMS) | |
$Context.ExecuteQuery() | |
#Get Term Stores | |
$TermStores = $MMS.TermStores | |
$Context.Load($TermStores) | |
$Context.ExecuteQuery() | |
$TermStore = $TermStores[0] | |
$Context.Load($TermStore) | |
$Context.ExecuteQuery() | |
#Get Groups | |
$Group = $TermStore.Groups.GetByName($GroupName) | |
$Context.Load($Group) | |
$Context.ExecuteQuery() | |
#Bind to Term Set | |
$TermSet = $Group.TermSets.GetByName($TermSetName) | |
$Context.Load($TermSet) | |
$Context.ExecuteQuery() | |
#Create the file and add headings | |
$OutputFile = "Output File Path1.csv" | |
$file = New-Object System.IO.StreamWriter($OutputFile) | |
$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"); | |
$Terms = $TermSet.Terms | |
$Context.Load($Terms); | |
$Context.ExecuteQuery(); | |
$lineNum = 1; | |
Foreach ($Term in $Terms) | |
{ | |
if($lineNum -eq 1) | |
{ | |
##output term properties on first line only | |
$file.Writeline($TermSet.Name + "," + $TermSet.Description + "," + $TermStore.DefaultLanguage + "," + $TermSet.IsAvailableForTagging + "," + $Term.Description + "," + $Term.Name + "," + "," + "," + "," + "," + "," ); | |
} | |
else | |
{ | |
$file.Writeline("," + "," + "," + "," + $Term.Description + "," + $Term.Name + "," + "," + "," + "," + "," + "," ); | |
} | |
$lineNum = $lineNum + 1; | |
$TermTreeLevel = 1; | |
GetTerms -Term $Term -Level $TermTreeLevel -ParentTerm ""; | |
} | |
$file.Flush(); | |
$file.Close(); | |
After running the script for the term set “Document Classification” in group “Intranet”, the output csv file is produced similar to the sample import file.
The file can be imported using the “Import Term Set” functionality on a different environment.