SharePoint Online: Import Terms to a Term Set from CSV using PowerShell
Requirement: Import terms to a term set in SharePoint Online from a CSV file using PowerShell.
How to Import Terms to a Term Set in SharePoint Online?
In this post, we’ll show you how to bulk-create terms in a SharePoint Online term set. This can be helpful If you need to quickly add a large number of terms to a term set. SharePoint offers an OOTB way to Import a term set from a CSV, but there are no ways to create just terms. So, We’ll be using a CSV file with a bunch of terms as the input and PowerShell to create terms in a term set.
Here is my CSV file with a list of Terms for a Term set called “Job Title” and we can use PowerShell to bulk create terms for a term set.
SharePoint Online Import Terms to Term Set using PowerShell:
If you want to bulk-add terms to a term set, PowerShell can help! This blog post will show you how to import terms to a term set from CSV 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"
$TermGroupName= "People"
$TermSetName="Job Title"
$CSVFile ="C:\Temp\ImportTerms.csv"
$TermHeaderInCSV ="JobTitle"
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
#Get the term store
$TaxonomySession=[Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($Ctx)
$TaxonomySession.UpdateCache()
$TermStore =$TaxonomySession.GetDefaultSiteCollectionTermStore()
$Ctx.Load($TaxonomySession)
$Ctx.Load($TermStore)
$Ctx.ExecuteQuery()
#Get Termstore data from CSV and iterate through each row
Import-Csv $CSVFile | ForEach-Object {
#Get the Term Group
$TermGroup=$TermStore.Groups.GetByName($TermGroupName)
#Get the term set
$TermSet = $TermGroup.TermSets.GetByName($TermSetName)
#CSV File Header Row in Term to Add
$TermName = $_.$($TermHeaderInCSV)
#Check if the given term exists already
$Terms = $TermSet.Terms
$Ctx.Load($Terms)
$Ctx.ExecuteQuery()
$Term = $Terms | Where-Object {$_.Name -eq $TermName}
If(-not $Term)
{
#Create Term Set
Write-host "Creating Term '$TermName'" -ForegroundColor Cyan
$Term = $TermSet.CreateTerm($TermName,1033,[System.Guid]::NewGuid().toString())
$Ctx.Load($Term)
$Ctx.ExecuteQuery()
$Term.TermStore.CommitAll()
$TaxonomySession.UpdateCache()
Write-host "New Term '$TermName' Added Successfully!" -ForegroundColor Green
}
else
{
Write-host "Term '$TermName' Exists Already!" -ForegroundColor Yellow
}
}
}
Catch {
write-host -f Red "Error Importing Term store Data!" $_.Exception.Message
}
And the result:
If you want to import Term Groups, Term Sets, and Terms from a CSV, use: SharePoint Online: How to Add Terms, Term Sets, and Term Groups from CSV using PowerShell
Great, thanks! Although I have a question in relation to this method – I’ll post on that page.
Hi Salaudeen.
Just wondering is it possible to modify this script to actually import the items to a Term below the Term Set? So, my structure currently has (just for example);
* Cafe (Group Name)
* Drinks (Term Set)
* Coffee (Term)
* …other Terms with types of Coffee
So, can I import into the “Coffee” Term?
Yes, You can create up to 8 levels of deep child terms! Here is the reference: SharePoint Online: Create Child Terms in Term Store using PowerShell
hello, i had the error below with your script
Error Importing Term store Data! Exception lors de l’appel de « ExecuteQuery » avec « 0 » argument(s) : « Specified argument was out of the range of valid values.
Parameter name: index »
This means: The given Term Group or Term set doesn’t exist!
This is very helpful. What changes are necessary to adapt your script to include the term description?
Great thanks. I am seeing following error: Error Importing Term store Data ! Exception calling ExecuteQuery with 0 aguments: “Access denies. You do not have permission to perform this action or access this resource.” Any idea about this?
You need to be a member of “Term Store Administrator” group even you are a Tenant Admin! So try adding the account you are running the PowerShell script to Term Store Admin group.