SharePoint Online: Create Child Terms in Term Store using PowerShell

Requirement: Create a child term in SharePoint Online term store.

How to Add Child Terms in SharePoint Online? 

SharePoint Online term store provides an easy interface to manage term store data in hierarchical manner. We can create child terms (or sub-terms) on existing terms in the termstore.

  • You can create child term on any existing terms in termstore by selecting "Add Term" from any existing term's context menu.
    sharepoint online create child terms



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 -UseWebLogin
    
    $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
}

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.