SharePoint Online / Office 365 : Exporting All The Terms From Particular TermSet In .csv file Using CSOM & PowerShell

Scenario

We have our SharePoint online site. We are using TermSets in one of the our projecs. We have one TermSet called “Department” having around 40-50 terms (each term is one department). There are sudden changes in the department names, so our customer wants to go through each term and wants to correct those.

Here, one option is to give the rights to the customer so that they can go to the “Term Store Management” page (_layouts/15/termstoremanager.aspx) and change the default label for the terms which they want to change. But this is not the best approach. This approach may be confusing for non technical customers.

So the best alternative we thought is that we will provide TermSet in .CSV file (excel) and give it to the customer, they will do the respective changes and send them back to us so that we can directly import those.

But again, OOB there is no option to Export the given TermSet as shown in Figure 1 and copying each term manually is not the best possible solution again.

OPTION
Figure 1: No “Export Term Set” option

So one of the best possible approaches is to write our own PowerShell script and export the termms of the TermSet in the .CSV file.

PowerShell script Step By Step

Step 1: Load the Taxonomy library as,

#Load the Taxonomy library

Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll" # Path to Taxonomy library (where client SDK is installed)

Step 2:

Set the required variables like SiteCollection URL from where we need to export the TermSet, UserName and Password of your site to connect as,

#Site collection URL from where we need to export the TermSet,

$siteurl = "" # Your site collection URL

#User name and Passwords

$userName ="" # Your user name
$password ="" # Password

Step 3: Get the Microsoft.SharePoint.Client.ClientContext instance and set the credentials as,

#client context object and setting the credentials
[Microsoft.SharePoint.Client.ClientContext]$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($siteurl)

# convert password into secured string
$securedpw = ConvertTo-SecureString $password -AsPlainText -Force

$clientContext.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $securedpw)

Step 4: Get / Load the required web object as ,

# Retrieving the taxonomy session

  1. $taxonomysession = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($clientContext)  
  2.   
  3. $termStore = $taxonomysession.GetDefaultKeywordsTermStore()  
  4. $groups =$termStore.Groups  
  5.   
  6. $context.Load($taxonomysession)  
  7. $context.Load($termStore)  
  8. $context.Load($groups)   
Step 5 : Get the group from which we need to export the TermSet,

#Get the group from which we need to export the TermSet

$group = $groups.GetByName("Prasham") #Prasham is group name - as shown in Fig1

#Retrieving the Term Set collection

$termSets = $group.TermSets

#Retrieving the "Department" term set

$termSet = $termSets.GetByName("Department")

#Retrieving all terms
  1. $terms = $termSet.Terms  
  2.   
  3. $context.Load($group)  
  4. $context.Load($termSets)  
  5. $context.Load($termSet)  
  6. $context.Load($terms)  
  7. $context.ExecuteQuery()  
#Loop through each term
  1. foreach($term in $terms)  
  2. {  
  3. #Your .CSV file name in which you need to write the Term   
  4. Write-Output $term.Name >> "C:\test.csv"  
  5. }   

Read more articles on SharePoint:

Similar Articles