Skip to main content

SharePoint Online - Export Term Store data

We had a requirement to export Terms from a particular Term Store Group in our SPO tenant but I couldn't find a script that goes in to Level 2 and Level 3 Terms or if there is one out there I couldn't find it.

I checked many forums and blogs and thanks to all those contributors who inspired me to come up with this script. This isn't perfect by any means and I would like to improve and clean this up a bit but I'm sharing this here so its documented where I can fix this later and also probably help someone.

The script will export the data in hierarchical form in a .csv file.

 

# Tenant url

$url="<enter your SharePoint admin url>"

# Term Group names as an array

$groups = @("") 

# location of CSV file

$FilePath="Terms.csv" 

# Variable to collect all data and export to CSV

$results = New-Object System.Collections.ArrayList

# Function to iterate through Term store

function Get-TermsAsCSV

{      

    try  

    {  

        if($groups.Length -eq 0){

            $groups = @(Get-PnPTermGroup | ForEach-Object{ $_.Name })

        }

        # Loop through the term groups

        foreach ($termGroup in $groups) {

            try {

                $termGroupName = $termGroup


                # Exporting terms from Term Group

                $termGroupObj = Get-PnPTermGroup -Identity $termGroupName -Includes TermSets, Description                   

                 foreach ($termSet in $termGroupObj.TermSets) {                    

                    # variable to store Term Store within the Term Group

                    $termSetObj = Get-PnPTermSet -Identity $termSet.Id -TermGroup $termGroupName -Includes Terms, Description, IsAvailableForTagging

                     $Data = @{

                    'Term group' = $termGroupName                    

                    'Term set Name'   = $termSetObj.Name   

                    'Term Set Description' = $termSetObj.Description 

                    'Available For Tagging' = $termSetObj.IsAvailableForTagging           

                    'Term 1'     = ''

                    'Term 2'     = ''

                    'Term 3'     = '' }                      

                    $results.add( (New-Object psobject -Property $Data) )                          

                    # Iterate through Terms within Term Store

                    foreach ($terms in $termSetObj.terms) {                                   

                    Write-Host "Term: " $terms.Name

                    $children = Get-PnPTerm -TermSet $termSetObj.Name -TermGroup $termGroupName -Identity $terms.Name -IncludeChildTerms -Includes IsAvailableForTagging, Description #-Recursive

                    $Data = @{

                    'Term group' = ''                    

                    'Term set Name'   = $termSetObj.Name

                    'Term Set Description' = $terms.Description

                    'Available For Tagging' = $terms.IsAvailableForTagging

                    'Term 1'     = $terms.Name

                    'Term 2'     = ''

                    'Term 3'     = '' } 

                    $results.add( (New-Object psobject -Property $Data) )

                     # Iterate through if child Terms exist and adding them to Array

                    if($children.Terms.Count -gt 0)

                    {

                    foreach($child in $children.Terms)

                    {                        

                    $Data = @{

                    'Term group' = ''                    

                    'Term set Name'   = $termSetObj.Name

                    'Term Set Description' = ''

                    'Available For Tagging' = $termSetObj.IsAvailableForTagging

                    'Term 1'     = $terms.Name

                    'Term 2'     = $child.Name

                    'Term 3'     = '' } 

                    $results.add( (New-Object psobject -Property $Data) )

                    # This validation prevents error if there aren't any child terms for a particular Term

                    if($child.Terms.Count -gt 0)

                       {

                       # Iterate through next level child Terms and adding them to Array

                        foreach($c in $child.Terms){                   

                        $Data = @{

                        'Term group' = ''                        

                        'Term set Name'   = $termSetObj.Name

                        'Term Set Description' = ''

                        'Available For Tagging' = $termSetObj.IsAvailableForTagging

                        'Term 1'     = $terms.Name

                        'Term 2'     = $child.Name

                        'Term 3'     = $c.Name }


                        $results.add( (New-Object psobject -Property $Data) )}

                       }                    

                    }

                    }

                  }

                }

            }

            catch {Write-Host "Error1: $($_.Exception.Message)" -ForegroundColor Red}            

        }

     }  

     catch [Exception]  

     {  

        $ErrorMessage = $_.Exception.Message         

        Write-Host "Error2: $ErrorMessage" -ForegroundColor Red          

     }  

}  

# Connect to SharePoint Online site

Connect-PnPOnline -Url $Url -Interactive


## Call the Function  

Get-TermsAsCSV


$results | Select-Object  'Term group','Term set Name', 'Term Set Description','Available For Tagging','Term 1', 'Term 2','Term 3' | Export-Csv -Path $FilePath -NoTypeInformation -Encoding UTF8

# Disconnect the session

Disconnect-PnPOnline  


Comments