Yes/No Column PnP PowerShell

PnP PowerShell: How to create Term Set in SharePoint Online?

Introduction

This blog post will discuss term sets and different term set types according to their scope. Along with this, we will also go through how to create a term set in SharePoint Online using PnP PowerShell.

In SharePoint, a term set is a collection of linked terms that are formed in a layered structure. For instance, a country term may be followed by a list of states, and each state may then be followed by a list of cities. Terms can be used as metadata in lists and libraries to tag the content (items in the list/documents) with these terms. Term sets are also thought of as the classification or categorization of the information.

Term sets can be created at the tenant or site collection levels. Term sets created at the tenant level are referred to as “Global Term Sets” because they are accessible to all site collections in that tenant, whereas term sets created at the site collection level are referred to as “Local Term Sets” because they are unique to that site collection.

Prerequisites

Before we begin, ensure that the PnP PowerShell module is already installed. If you need assistance installing the PnP PowerShell, please see this article: https://www.hemantkabra.in/microsoft-sharepoint/pnp-powershell-how-to-get-started/

To create a new term set, you must also have term store administrator or contributor permissions.

Creation of Term Sets

We will now create two types of term sets: global term sets (at the tenant level) and local term sets (at Site Collection Level)

Global Term Set (Tenant Level)

See below code to create a global term set at tenant level:

# SharePoint Admin Center URL
$SPAdminCenterURL = "https://contoso-admin.sharepoint.com"

# Name of the Term Group
$TermGroupName = "Departments"

# Connect to the SharePoint Admin Center using Connect-PnPOnline cmdlet
Connect-PnPOnline -Url $SPAdminCenterURL -Interactive
 
# Create a new Term Set
New-PnPTermSet -Name "Technology" -TermGroup $TermGroupName

Few important points to consider about above example:

  • We are creating a global term set, so we are connecting to the SharePoint Admin Center. Replace it with you tenant URL.
  • This will create a Technology term set inside Departments Term Group.
  • While using New-PnPTermSet, you can configure few other properties as well such as Description, if Term Set is available for tagging or open for terms creation etc.

Local Term Set (Site Collection Level)

To create a local term set, we will follow nearly the same steps as we did to create the global term set. The only difference is that we will connect to the site collection rather than the tenant, and we will use the default site collection term group. For more details, see the code below:

# Site Collection URL
$SiteUrl = "https://contoso.sharepoint.com/sites/departments"

# Connect to the SharePoint Site Collection using Connect-PnPOnline cmdlet
Connect-PnPOnline -Url $SiteUrl -Interactive

# Get Site Context
$Context = Get-PnPContext

# Get taxonomy session
$TaxSession = Get-PnPTaxonomySession

# Get default site collection term store
$TermStore = $TaxSession.GetDefaultSiteCollectionTermStore()

# Get the site collection group
$TermGroup = $TermStore.GetSiteCollectionGroup($Context.Site, $true)
$Context.Load($TermGroup)
$Context.ExecuteQuery()
 
# Create a new Term Set
New-PnPTermSet -Name "Topics" -TermGroup $TermGroupName

Few points to consider about above example:

  • Replace the Site Collection URL where you would like to create a local term set.
  • This will create Topics term set in default site collection term group.

Official Documentation

Visit the official documentation at https://pnp.github.io/powershell/cmdlets/New-PnPTermSet.html to learn more about the New-PnPTermSet cmdlet and to explore the additional parameters that it supports.

I hope you enjoyed reading this blog post. Please feel free to leave a comment in case you have any feedback, suggestions, or queries.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *