SharePoint Online: Create Term in Term Store using Powershell

Requirement: Create a Term in SharePoint Online Term Store.

How to Create a Term in SharePoint Online Term Store?
Term store in SharePoint Online enables you to categorize and standardize data. You can define categories and tag your data for classification. To create a new term in SharePoint online term store, follow these steps:
  • Navigate to your SharePoint admin center site. (E.g. https://yourdomain-admin.sharepoint.com)
  • Click the "term store" link on the left navigation menu.
  • 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
    SharePoint Online Create Term in Term Store using Powershell
  • Start typing your new term, Hit enter to complete creating the term.
    PowerShell to Create New Term in SharePoint Online Term Store

PowerShell to Create New Term in SharePoint Online Term Store:
#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 specific term set in the term store. Lets 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 =@("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
#Config Variables
#$SiteURL = "https://crescenttech.sharepoint.com"
$AdminCenterURL = "https://crescenttech-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, terms (If they don't exist already!)
#Config Variables
$AdminCenterURL = "https://crescenttech-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" 
SharePoint Online: Create Term in Term Store using Powershell SharePoint Online: Create Term in Term Store using Powershell Reviewed by Salaudeen Rajack on November 06, 2016 Rating: 5

1 comment:

  1. 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

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.