SharePoint Online: Create a Term in Term Store using PowerShell
Requirement: Create a Term in SharePoint Online Term Store.
How to Add a Term in SharePoint Online Term Store?
The term store in SharePoint Online enables you to categorize and standardize data. You can define categories and tag your data for classification in the form of terms, term sets, and term groups. To create a new term in the SharePoint Online term store, follow these steps:
- Navigate to your SharePoint admin center site. (E.g., https://tenant-admin.sharepoint.com)
- Click the “term store” link on the left navigation menu. (In Modern SharePoint Admin Center, select Classic features, then click “Open” under Term store.)
- Expand and locate the Term set in which you want to add a new Term. Click on the little arrow in the Term set Header >> Click on “Create Term” option.
- Start typing your new term, and hit enter to complete creating the term.
To add multiple terms, simply press ENTER after each term. Once the term is created, you can specify additional properties for the new term. Such as Description, Available for tagging, Language, etc., in the properties pane and hit save once done.
PowerShell to add a New Term in SharePoint Online Term Store:
So, you probably want to automate the task of adding terms to your SharePoint Online Term Store using PowerShell. Here is how to add terms using PnP 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 ="Regions"
$TermSetName="MENA"
$TermName="United Arab Emirates"
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)
$TermStore =$TaxonomySession.GetDefaultSiteCollectionTermStore()
$Ctx.Load($TaxonomySession)
$Ctx.Load($TermStore)
$Ctx.ExecuteQuery()
#Get the Term Group
$TermGroup = $TermStore.Groups.GetByName($TermGroupName)
$Ctx.Load($TermGroup)
#Get the Term Set
$TermSet = $TermGroup.TermSets.GetByName($TermSetName)
$Ctx.Load($TermSet)
#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
$NewTerm = $TermSet.CreateTerm($TermName,1033,[System.Guid]::NewGuid().toString())
$Ctx.Load($NewTerm)
$Ctx.ExecuteQuery()
Write-host "New Term '$TermName' Created Successfully!" -ForegroundColor Green
}
else
{
Write-host "Term '$TermName' Exists Already!" -ForegroundColor Yellow
}
}
Catch {
write-host -f Red "Error Creating Term!" $_.Exception.Message
}
PowerShell to add Multiple Terms to Term set in SharePoint Online:
The above script adds a single term to a specific term set in the term store. Let us add multiple terms to a term set.
#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 ="Regions"
$TermSetName="MENA"
$TermsToAdd [email protected]("Bahrain", "Egypt", "Iran", "Iraq", "Jordan", "Kuwait", "Lebanon", "Oman", "Palestine", "Qatar", "Saudi Arabia", "Syria", "Turkey", "United Arab Emirates", "Yemen")
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)
$TermStore =$TaxonomySession.GetDefaultSiteCollectionTermStore()
$Ctx.Load($TaxonomySession)
$Ctx.Load($TermStore)
$Ctx.ExecuteQuery()
#Get the Term Group
$TermGroup = $TermStore.Groups.GetByName($TermGroupName)
$Ctx.Load($TermGroup)
#Get the Term Set
$TermSet = $TermGroup.TermSets.GetByName($TermSetName)
$Ctx.Load($TermSet)
#Create Terms
Foreach ($TermName in $TermsToAdd)
{
#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 new Term
$NewTerm = $TermSet.CreateTerm($TermName,1033,[System.Guid]::NewGuid().toString())
$Ctx.ExecuteQuery()
$TermStore.CommitAll()
Write-host "New Term '$TermName' Added Successfully!" -ForegroundColor Green
}
else
{
Write-host "Term '$TermName' Exists Already!" -ForegroundColor Yellow
}
}
}
Catch {
write-host -f Red "Error Creating Term!" $_.Exception.Message
}
SharePoint Online: Create Term using PnP PowerShell
Let’s use PnP PowerShell to add a Term to a Term set in SharePoint Online:
#Config Variables
#$SiteURL = "https://Crescent.sharepoint.com"
$AdminCenterURL = "https://Crescent-admin.sharepoint.com"
$TermGroupName = "Regions"
$TermSetName = "MENA"
$TermName="United Arab Emirates"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
#Check if Term exists
If(-Not(Get-PnPTerm -Identity $TermName -TermSet $TermSetName -TermGroup $TermGroupName -ErrorAction SilentlyContinue))
{
#Create new Term
New-PnPTerm -Name $TermName -TermSet $TermSetName -TermGroup $TermGroupName
}
Else
{
Write-host -f Yellow "Term '$TermName' already exists!"
}
What If the given Term Group or Term Set doesn’t exist? Well, you’ll end up in error! So, Here is how you can mitigate: The following PnP PowerShell imports Term store data. It creates term group, term set, and terms (If they don’t exist already!)
#Config Variables
$AdminCenterURL = "https://Crescent-admin.sharepoint.com"
$Taxonomy= "Deals Pipeline|Regions|South America" #Format: Term Group|Termset|Term
#Connect to PnP Online
Connect-PnPOnline -Url $AdminCenterURL -Credentials (Get-Credential)
#Import Term store Group, Term Set and Terms
Import-PnPTaxonomy -Terms $Taxonomy
The Import-PnPTaxonomy also supports importing from arrays and even text files! E.g.,
$Taxonomy= "Deals Pipeline|Regions|Africa","Deals Pipeline|Regions|South America","Deals Pipeline|Regions|North America"
In summary, creating a term in SharePoint Online can be done through the user interface or PowerShell. You can easily create a term and define its properties using the above methods. Terms are an important part of Managed Metadata Service in SharePoint Online and can be used to classify and organize content.
Great!
Is there any way to update taxonomy Hidden List using client object model.
“How to update the taxonomy Hidden List or how to force rerun the Taxonomy Update scheduler job to Sync updates to Taxonomy Hidden list”
Regards,
Mohammad Amer