SharePoint Online: Create Child Terms in Term store using PowerShell
Requirement: Create a child term in the SharePoint Online term store
How to Add Child Terms in SharePoint Online?
In SharePoint Online, you can add child terms to a term in a term set to Up to 7 levels down, to create a hierarchical organization of terms or break out terms into specific categories. There are a few different ways to add child terms to a term – with Term Store Manager or PowerShell cmdlets. In this blog post, we will look at each option and show you how to add child terms using each method.
SharePoint Online term store provides an easy interface to manage term store data hierarchically. We can create child terms (or sub-terms) on existing terms in the term store.
- You can create child terms on any existing terms in termstore by selecting “Add Term” from any existing term’s context menu.
PowerShell to Add Child Terms in SharePoint Online
Here is the PowerShell way to create child terms in SharePoint Online:
#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"
#Parameters
$TenantURL = "https://crescent.sharepoint.com"
$TermGroupName = "Classification"
$TermSetName = "Tags"
$ParentTermName = "Continent"
#Function to Add Child Term (or Sub-term)
Function Add-ChildTerm
{
param (
[parameter(Mandatory=$true, ValueFromPipeline=$true)][string]$TermName,
[parameter(ValueFromPipelineByPropertyName = $true)][int]$Language = 1033,
[parameter(Mandatory=$true, ValueFromPipeline=$true)][Microsoft.SharePoint.Client.Taxonomy.Term]$ParentTerm,
[parameter(Mandatory=$true, ValueFromPipeline=$true)][Microsoft.SharePoint.Client.ClientContext]$Context
)
Try {
$ChildTerm = $ParentTerm.CreateTerm($TermName, $Language, [System.Guid]::NewGuid().toString())
$ParentTerm.TermStore.CommitAll()
$Context.ExecuteQuery()
Write-host "New Term '$TermName' Added Successfully!" -ForegroundColor Green
}
Catch {
write-host -f Red "Error Adding Term:" $_.Exception.Message
}
}
Try {
#Get Credentials to connect
$Cred = Get-Credential
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($TenantURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#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)
#Get the Parent term
$Term = $TermSet.Terms.GetByName($ParentTermName)
$Ctx.Load($Term)
$Ctx.ExecuteQuery()
#Call the function to Create a Child Term
Add-ChildTerm -TermName "America" -ParentTerm $Term -Context $Ctx
}
Catch {
write-host -f Red "Error:" $_.Exception.Message
}
Create Child Terms in SharePoint Online using PnP PowerShell
We can also create sub-terms using PnP PowerShell:
#Parameters
$TenantURL = "https://crescent.sharepoint.com"
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $TenantURL -Interactive
$ParentTerm = Get-PnPTerm -TermGroup "Classification" -TermSet "Tags" -Identity "Continent" -IncludeChildTerms -Recursive
$ChildTerm = $ParentTerm.CreateTerm("Asia", 1033, (New-Guid))
Invoke-PnPQuery
}
Catch {
Write-Host -f Red "Error:"$_.Exception.Message
}
Hi Salaudeen.
This look great but how do I import bulk items using the above method? In this post (https://www.sharepointdiary.com/2016/11/sharepoint-online-import-terms-to-termset-using-powershell.html), I can reference a .CSV file. Can we do the same with this script?
Cheers
Kelly