SharePoint Online: Create New Term Set using PowerShell

Requirement: Create a New Term Set in SharePoint Online Term Store.

How to Create a New Term Set in SharePoint Online?
Term sets are a set of Terms stored in a hierarchical structure. E.g: Country > State > City. In other words, Terms are stored in term sets. Term sets in managed metadata service in SharePoint can be considered as a category or classification with a number of predefined terms that can be used as metadata in lists or libraries. Follow these steps to create a term set in SharePoint Online:
  • 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, and then click on "Open" under Term store. Make sure you are adding yourself to "Term Store Administrators" of the Term Store, before trying to make any change.
  • If you don't have existing term groups created, you can click on the "Taxonomy_<id>" root node and then select "New Group" to create a new term group first.
  • From the taxonomy tree view, Select the term group in which you want to add a term set. Click on the little arrow in the Term group Header >> Click on New Term Set option
    powershell to create term set in sharepoint online
  • Start typing your term set. Hit the Enter button to complete creating the term set.
    SharePoint Online Create Term Set using PowerShell
  • That's all! Term sets can be created either in a site collection or at tenant level for the SharePoint Admin center to Make a term set available across all site collections.
Once the term set is created, You can set the properties of the term set, such as Term set Name, description, owner, etc.
To create and manage terms and term sets in the Term Store management tool, you must be a Contributor, a Group Manager, or a Term Store Administrator!

PowerShell to Create a Term Set in SharePoint Online:
Here is how to add a term set 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"
  
#Variables for Processing
$AdminURL = "https://crescent-admin.sharepoint.com/"
$TermGroupName ="Regions"
$TermSetName="MENA"

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)

    #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
        $NewTermSet = $TermGroup.CreateTermSet($TermSetName,[System.Guid]::NewGuid().toString(),1033)
        $Ctx.Load($NewTermSet)
        $Ctx.ExecuteQuery()
    
        Write-host "Term Set '$TermSetName' Created Successfully!" -ForegroundColor Green
    }
    else
    {
        Write-host "Term Set '$TermSetName' Exists Already!" -ForegroundColor Yellow
    }
}
Catch {
    write-host -f Red "Error Adding Term Group!" $_.Exception.Message
}
This creates a new term set in the given term group of the SharePoint Online Term store.

SharePoint Online: PnP PowerShell to Create Term Set
Here is how to create a term set in SharePoint Online using PnP PowerShell
#Config Variables
$AdminCenterURL = "https://crescenttech-admin.sharepoint.com"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Create new Term Set
New-PnPTermSet -Name "Region" -TermGroup "Deals Pipeline" 
This creates a new term set "Regions" in SharePoint Online!

Once the term set is ready, you can start adding terms to it: SharePoint Online: Create a Term in Term Store using Powershell

1 comment:

Please Login and comment to get your questions answered!

Powered by Blogger.