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/

8 Comments

    • Jens said:

      Hi

      Thanks for you top export Script 🙂
      i receive an error :

      [System.Collections.ArrayList] $completeTerm = [System.Collections.ArrayList]::new()
      Method invocation failed because [System.Collections.ArrayList] does not contain a method named ‘new’.
      At line:1 char:1
      + [System.Collections.ArrayList] $completeTerm = [System.Collections.ArrayList]::n …
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidOperation: (:) [], RuntimeException
      + FullyQualifiedErrorId : MethodNotFound

      What’s wrong?

      Thanks for your assistance
      Jens

      June 22, 2021
      Reply
      • David Ramalho said:

        Hi,

        Seems that your PowerShell is loading the System.Collections.ArrayList library, try this instead $completeTerm = @()

        My best,
        David Ramalho

        October 21, 2021
        Reply
  1. Christopher Mainland said:

    this ony works for two levels

    June 4, 2021
    Reply
    • David Ramalho said:

      Hi,

      In the description of the article is highlighted that in this particular scenario that the code is only ready for 2 levels of the term store.

      My best,
      David Ramalho

      October 21, 2021
      Reply
  2. Brett said:

    Does this work for SP2016 on-prem?
    Error message: Connect-PnPOnline : The term ‘Connect-PnPOnline’ is not recognized as the name of a cmdlet, function, script file, or operable program.

    July 28, 2021
    Reply
    • Brett said:

      I kind of got around that error using this: Install-Module SharePointPnPPowerShellOnline -Scope CurrentUser

      July 28, 2021
      Reply
  3. Ben Cale said:

    Great bit of code but in the csv you have the columns “Level 3 Term”, “Level 4 Term”. but there is no code to populate them?
    The only code I can see is termStore.Name; and $termStoreChild.Name for the first 2 levels, is there an equivalent for levels 3 and 4?

    November 3, 2021
    Reply

Leave a Reply

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