Export SharePoint Term Set to CSV


Photo by PDPics on Pixabay

This is one of the most used features of SharePoint and sometimes, we may create this term set on a testing site and we need to transfer this term set to other sites. I’ve created a PowerShell script that allows you to export this to a CSV file. You can then use SharePoint OOTB functionality to import the term set to your taxonomy somewhere else.

This script needs to be adjusted with your credentials, the site where the term store is located in the case is configured to one site. Then select the TermStore Group and TermSet that you want to extract to the CSV. This code will is ready to get 2 levels of Term sets. If you have more levels, you need to adjust this code to get more items.

The Term store group used on this script is the People and the term set that we want to export is Department below image with the sample.

$userName = 'contoso'
$password = 'pw' | ConvertTo-SecureString -Force -AsPlainText
$cred = New-Object -typename System.Management.Automation.PSCredential($userName, $password)
$siteCollectionUrl = "https://contoso.sharepoint.com/sites/site"
[System.Collections.ArrayList] $completeTerm = [System.Collections.ArrayList]::new()


$termGroup = "People"
$termSet = "Department"

Connect-PnPOnline $siteCollectionUrl -Credentials $cred 
$termStores = Get-PnPTerm -TermGroup $termGroup -TermSet $termSet -IncludeChildTerms 
$completeTerm = @()

foreach ($termStore in $termStores) {
 $completeTerm += New-Object PsObject -Property @{
  "Term Set Name" = $termSet;
  "Level 1 Term"  = $termStore.Name;
 }
 if ($termStore.TermsCount -gt 0) {
  foreach ($termStoreChild in $termStore.Terms) {
   $completeTerm += New-Object PsObject -Property @{ 
    "Term Set Name" = $termSet;
    "Level 1 Term"  = $termStore.Name;
    "Level 2 Term"  = $termStoreChild.Name;
   }	
  }
 }
}
$completeTerm | Select-Object "Term Set Name", "Term Set Description", "LCID", "Available for Tagging", "Term Description", "Level 1 Term", "Level 2 Term", "Level 3 Term", "Level 4 Term" | Export-Csv "./TermSet.csv" -NoTypeInformation

Conclusion

With this script is easy to export the term set on the SharePoint. It’s also possible to adjust the code to retrieve more terms. If you want to import the term store verify this article on option number 2 –
https://sharepointmaven.com/3-ways-to-add-terms-to-the-term-store-in-sharepoint/

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *