SharePoint Online: Export Term Store Data to CSV using PowerShell

Requirement: Export Term store data from SharePoint Online to a CSV file

Solution: There is no out of box way to export complete term store data in SharePoint online. However, we can utilize PowerShell to export term store data including all term groups, term sets and terms to a CSV file. Here is the script.
sharepoint online export term set

PowerShell to Export Term Store data to CSV:
Let's export term store in SharePoint Online using PowerShell
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll"
  
#Variables for Processing
$AdminURL = "https://crescent-admin.sharepoint.com/"
$ReportOutput="C:\Temp\TermStoreData.csv"

Try {
    #Get Credentials to connect
    $Cred = Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminURL)
    $Ctx.Credentials = $Credentials

     #Array to Hold Result - PSObjects
    $ResultCollection = @()

    #Get the term store
    $TaxonomySession=[Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($Ctx) 
    $TermStore =$TaxonomySession.GetDefaultSiteCollectionTermStore()
    $Ctx.Load($TaxonomySession)
    $Ctx.Load($TermStore)
    $Ctx.ExecuteQuery()

    #Get all term groups   
    $TermGroups = $TermStore.Groups
    $Ctx.Load($TermGroups)
    $Ctx.ExecuteQuery()

    #Iterate through each term group
    Foreach($Group in $TermGroups)
    {
        #Get all Term sets in the Term group
        $TermSets = $Group.TermSets
        $Ctx.Load($TermSets)
        $Ctx.ExecuteQuery()

        #Iterate through each termset
        Foreach($TermSet in $TermSets)
        {
            #Get all Terms from the term set
            $Terms = $TermSet.Terms
            $Ctx.Load($Terms)
            $Ctx.ExecuteQuery()

            #Iterate through each term
            Foreach($Term in $Terms)
            {
                $TermData = new-object PSObject
                $TermData | Add-member -membertype NoteProperty -name "Group" -Value $Group.Name
                $TermData | Add-member -membertype NoteProperty -name "TermSet" -Value $Termset.Name
                $TermData | Add-member -membertype NoteProperty -name "Term" -Value $Term.Name   
                $ResultCollection += $TermData
            }
        }
    }
    #export term store sharepoint online powershell
    $ResultCollection | Export-csv $ReportOutput -notypeinformation

    Write-host "Term Store Data Successfully Exported!" -ForegroundColor Green   
}
Catch {
    write-host -f Red "Error Exporting Termstore Data!" $_.Exception.Message
}
This produces a CSV file as in this image: It exports all first level terms from the term store.
sharepoint online powershell export term set

Export All Terms from All Levels from the Term Store to CSV:
What if the terms in Term store have children up to 7 levels?
sharepoint online powershell to export term store to CSV

This PowerShell script exports all terms from all levels to CSV. Terms are separated by semicolon (;) 
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll"
   
#Variables for Processing
$AdminURL = "https://crescent-admin.sharepoint.com/"
$ReportOutput="C:\Temp\TermStoreData.csv"

#Function to get all child terms from a Term
Function Get-SPOChildTerms([Microsoft.SharePoint.Client.Taxonomy.Term]$Term)
{
    $TermsColl = @()
    $TermsColl += $Term.PathOfTerm

    #Get All child terms
    $ChildTerms = $Term.Terms
    $Ctx.Load($ChildTerms)
    $Ctx.ExecuteQuery()
 
    #Process all child terms
    Foreach ($ChildTerm in $ChildTerms)
    {
        Get-SPOChildTerms($ChildTerm)
    }
    Return $TermsColl
}

Try {
    #Get Credentials to connect
    $Cred = Get-Credential
 
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
     #Array to Hold Result - PSObjects
    $ResultCollection = @()
 
    #Get the term store
    $TaxonomySession=[Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($Ctx) 
    $TermStore =$TaxonomySession.GetDefaultSiteCollectionTermStore()
    $Ctx.Load($TaxonomySession)
    $Ctx.Load($TermStore)
    $Ctx.ExecuteQuery()
 
    #Get all term groups   
    $TermGroups = $TermStore.Groups
    $Ctx.Load($TermGroups)
    $Ctx.ExecuteQuery()
 
    #Iterate through each term group
    Foreach($Group in $TermGroups)
    {
        #Get all Term sets in the Term group
        $TermSets = $Group.TermSets
        $Ctx.Load($TermSets)
        $Ctx.ExecuteQuery()
 
        #Iterate through each termset
        Foreach($TermSet in $TermSets)
        {
            Write-host "Getting Terms from Term Set '$($Termset.Name)' from Term Group '$($Group.Name)'" -f Yellow
            #Get all Terms from the term set
            $Terms = $TermSet.Terms
            $Ctx.Load($Terms)
            $Ctx.ExecuteQuery()

            #Iterate through each term
            Foreach($Term in $Terms)
            {
                #Get the child terms of the term
                $ChildTerms = Get-SPOChildTerms($Term)

                ForEach($Child in $ChildTerms)
                {
                    $TermData = new-object PSObject
                    $TermData | Add-member -membertype NoteProperty -name "Group" -Value $Group.Name
                    $TermData | Add-member -membertype NoteProperty -name "TermSet" -Value $Termset.Name              
                    $TermData | Add-member -membertype NoteProperty -name "Terms" -Value $Child
                    $ResultCollection += $TermData
                }
            }
        }
    }
    $ResultCollection
    #Export Results to a CSV File
    $ResultCollection | Export-csv $ReportOutput -NoTypeInformation -Force
 
    Write-host "Term Store Data Successfully Exported!" -ForegroundColor Green   
}
Catch {
    write-host -f Red "Error Exporting Termstore Data!" $_.Exception.Message
}
If you want to export all term sets to SharePoint Online compatible CSV (So that you can use the "Import" feature from term store), use my another script: SharePoint Online: Export Term Set to CSV using PowerShell

PnP PowerShell to Export Term Store Data to XML:
Here is how to export term store in SharePoint Online using PnP PowerShell
#Config Variables
$AdminCenterURL = "https://crescenttech-admin.sharepoint.com"

#Connect to PnP Online
Connect-PnPOnline -Url $AdminCenterURL -Credentials (Get-Credential)

#Export Term Store Data to XML
Export-PnPTermGroupToXml -Out "C:\Temp\TermStoreData.xml"

To import this CSV data to any other term store, use my another article: PowerShell to Import Term Store Data from CSV in SharePoint Online
SharePoint Online: Export Term Store Data to CSV using PowerShell SharePoint Online: Export Term Store Data to CSV using PowerShell Reviewed by Salaudeen Rajack on December 15, 2016 Rating: 5

5 comments:

  1. Thank you very much for your script! Unfortunately it runs into a timeout error: "Error Exporting Termstore Data! Ausnahme beim Aufrufen von "ExecuteQuery" mit 0 Argument(en): "The request channel timed out while waiting for a reply after 00:00:3
    0. Increase the timeout value passed to the call to Request or increase the SendTimeout value on the Binding. The time allotted to this operation may have been a por
    tion of a longer timeout."

    Can you help?

    ReplyDelete
    Replies
    1. This occurs usually on long running operations on slow connections. Try setting: $Ctx.RequestTimeOut = 5000*10000

      Delete
  2. Thanks for the post but this only return the first level of terms.

    ReplyDelete
    Replies
    1. Post is updated with an additional script to export all terms from all levels!

      Delete
  3. Is there a way to export a specific term group with all terms from all levels to CSV?

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.