SharePoint Online: Bulk Add Terms, Term Sets, and Term Groups from CSV using PowerShell
Requirement: Bulk Add Terms, Term Sets, and Term Groups from a CSV File into SharePoint Online term store.
Here is the format and data to import:
PowerShell Script to Bulk Add Terms, Term Sets, and Groups to SharePoint Online Term Store:
If you have a large number of taxonomy to add to your Term store, you can use PowerShell to automate the process. In this article, we will look at how to bulk add terms, term sets, and term groups from a CSV file using PowerShell. First, you need to prepare a CSV file with the term, term set, and term group information in the above format. Save the CSV file in a location that is accessible from the computer where you will be running the PowerShell script.
##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/"
$CSVFile = "C:\Users\salaudeen\Desktop\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
#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 {
$TermGroupName = $_.TermGroup
$TermSetName = $_.TermSet
$TermName = $_.Term
#**** Step 1: Create Term Group ****/
#Check if the given group exists already
$TermGroups = $TermStore.Groups
$Ctx.Load($TermGroups)
$Ctx.ExecuteQuery()
$TermGroup = $TermGroups | Where-Object {$_.Name -eq $TermGroupName}
If(-not $TermGroup)
{
#Create Term Group
Write-host "Creating Term Group '$TermGroupName'" -ForegroundColor Cyan
$TermGroup = $TermStore.CreateGroup($TermGroupName, [System.Guid]::NewGuid().toString())
$Ctx.Load($TermGroup)
$Ctx.ExecuteQuery()
$TermGroup.TermStore.CommitAll()
$TaxonomySession.UpdateCache()
Write-host "Term Group '$TermGroupName' Created Successfully!" -ForegroundColor Green
}
else
{
Write-host "Term Group '$TermGroupName' Exists Already!" -ForegroundColor Yellow
}
#**** Step 2: Create Term Set ****#
#Check if the given term set exists already
$TermSets = $TermGroup.TermSets
$Ctx.Load($TermSets)
$Ctx.ExecuteQuery()
$TermSet = $TermSets | Where-Object {$_.Name -eq $TermSetName}
If(-not $TermSet)
{
#Create Term Set
Write-host "Creating Term Set '$TermSetName'" -ForegroundColor Cyan
$TermSet = $TermGroup.CreateTermSet($TermSetName,[System.Guid]::NewGuid().toString(),1033)
$Ctx.Load($TermSet)
$Ctx.ExecuteQuery()
$TermSet.TermStore.CommitAll()
$TaxonomySession.UpdateCache()
Write-host "Term Set '$TermSetName' Created Successfully!" -ForegroundColor Green
}
else
{
Write-host "Term Set '$TermSetName' Exists Already!" -ForegroundColor Yellow
}
#*** Step 3: Create Term ***#
#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
}
PnP PowerShell to Import Term Store Data from a CSV
You can use PnP PowerShell to import term store data to SharePoint Online from a CSV file. Here is the PowerShell script to import Term Groups, Term Sets, and Terms from a CSV file.
#Parameters
$AdminCenterURL = "https://crescent-admin.sharepoint.com"
$CSVFile = "C:\Temp\TermStoreData.csv"
#Connect to Admin Center
Connect-PnPOnline -Url $AdminCenterURL -Interactive
#Get the Data from CSV
$CSVFile = Import-Csv $CSVFile
$RowCount = $CSVFile.Count
$Counter =1
#Iterate through each row in the CSV file
ForEach ($Row in $CSVFile)
{
# Get the term group
$TermGroup = Get-PnPTermGroup -Identity $Row.TermGroup -ErrorAction SilentlyContinue
if (!$TermGroup) {
# Create the term group if it doesn't exist
$TermGroup = New-PnPTermGroup -GroupName $Row.TermGroup
}
# Get the term set
$TermSet = Get-PnPTermSet -Identity $Row.TermSet -TermGroup $TermGroup -ErrorAction SilentlyContinue
if (!$TermSet) {
# Create the term set if it doesn't exist
$TermSet = New-PnPTermSet -Name $Row.TermSet -TermGroup $TermGroup
}
# Get the term
$Term = Get-PnPTerm -Identity $Row.Term -TermSet $Termset -TermGroup $TermGroup -ErrorAction SilentlyContinue
if (!$Term) {
# Create the term if it doesn't exist
$Term = New-PnPTerm -Name $Row.Term -TermSet $TermSet -TermGroup $TermGroup
}
Write-host "Processed Row $Counter out of $RowCount"
$Counter++
}
The PowerShell script above will loop through each row in the CSV file and do the following:
- Get the term group with the name specified in the “TermGroup” column. If the term group does not exist, it creates a new one using the New-PnPTermGroup cmdlet.
- Get the term set with the name specified in the “TermSet” column within the term group. If the term set does not exist, it creates a new one using the New-PnPTermSet cmdlet.
- Checks the term store to see if the given Term exists already in the specific term group, and term set. If not, add the term with the name specified in the “Term” column to the term set using the New-PnPTerm cmdlet.
Here is my CSV template: