SharePoint Online: Create Managed Metadata Site Column using PowerShell
Requirement: Add a Managed Metadata Site Column in SharePoint Online using PowerShell.
How to create a Managed Metadata Site Column in SharePoint Online?
Managed metadata is a feature in SharePoint that allows users to create and manage a controlled vocabulary of terms for organizing and categorizing content. Site columns in SharePoint provide a reusable way to store metadata and can be used to define the structure of content types and lists. In this article, we will explore how to use PowerShell to create a managed metadata site column in SharePoint.
The Managed Metadata column in SharePoint Online uses Term Set from the term store as the source for its allowed values. The Term set defines a set of values. For example, “Regions” is a Term set, and “Asia” is a Term. So, when the term set “Regions” is used as the source for a Managed Metadata site column, the term “Asia” can be its value. Here is how to add a managed metadata site column to the SharePoint Online list.
- Browse to your SharePoint Online site and Navigate to the site settings >> Site Columns
- Click on the “Create” link at the top of the page
- Provide the Name to your site column, specify the field type as “Managed Metadata”
- Scroll down and select the appropriate Term Set from the term store.
- Fill in other optional values and click on “OK” to create Managed Metadata site column in SharePoint Online.
PowerShell to Create Managed Metadata Site Column in SharePoint Online
Here is how to create a managed metadata column 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"
#Custom function to add Managed Metadata Site Column
Function Add-SPOManagedMetadataSiteColumn()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $ColumnName,
[Parameter(Mandatory=$true)] [string] $DisplayName,
[Parameter(Mandatory=$false)] [string] $Description = [string]::Empty,
[Parameter(Mandatory=$false)] [string] $IsRequired = $False,
[Parameter(Mandatory=$false)] [string] $EnforceUniqueValues = $False,
[Parameter(Mandatory=$true)] [string] $ColumnGroup,
[Parameter(Mandatory=$true)] [string] $TermGroupName,
[Parameter(Mandatory=$true)] [string] $TermSetName
)
#Generate new GUID for Field ID
$FieldID = ([GUID]::NewGuid()).GUID
Try {
$Cred= Get-Credential
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Get all Site columns from the site
$Fields = $Ctx.web.Fields
$Ctx.Load($Fields)
$Ctx.executeQuery()
#Check if the column name exists
$NewField = $Fields | where {$_.Title -eq $DisplayName -or $_.InternalName -eq $ColumnName}
if($NewField -ne $NULL)
{
Write-host "Site Column $ColumnName already exists!" -f Yellow
}
else
{
#Create Managed Metadata Column
$FieldSchema = "<Field Type='TaxonomyFieldType' ID='{$FieldID}' DisplayName='$DisplayName' Name='$ColumnName' Description='$Description' Required='$IsRequired' EnforceUniqueValues='$EnforceUniqueValues' Group='$ColumnGroup'/>"
$NewField = $Fields.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
$Ctx.ExecuteQuery()
Write-host "Site Column Created Successfully!" -ForegroundColor Green
#Get the Termset to Bind
$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)
#Get the term set
$TermSet = $TermGroup.TermSets.GetByName($TermSetName)
$Ctx.Load($TermSet)
$Ctx.ExecuteQuery()
#Bind Managed Metadata Termset to the column
$TaxField = [Microsoft.SharePoint.Client.ClientContext].GetMethod("CastTo").MakeGenericMethod([Microsoft.SharePoint.Client.Taxonomy.TaxonomyField]).Invoke($Ctx, $NewField)
$TaxField.SspId = $TermStore.Id
$TaxField.TermSetId = $TermSet.Id
$TaxField.Update()
$Ctx.ExecuteQuery()
Write-host "Site Column Binded to the Termset Successfully!" -ForegroundColor Green
}
}
Catch {
write-host -f Red "Error Creating Site Column!" $_.Exception.Message
}
}
#Define value for parameters
$SiteURL="https://Crescent.sharepoint.com"
$ColumnName="ProjectRegion"
$DisplayName="Project Regions"
$ColumnGroup="Crescent Projects"
$TermGroupName="Deals Pipeline"
$TermSetName="Regions"
#Call the function to create Managed Metadata Site Column
Add-SPOManagedMetadataSiteColumn -SiteURL $SiteURL -ColumnName $ColumnName -DisplayName $DisplayName -ColumnGroup $ColumnGroup -TermGroupName $TermGroupName -TermSetName $TermSetName
This script creates a new site column, “Project Regions,” and maps to a term set “Regions” under the “Deals” group in the Term Store.
PnP PowerShell to Add a Managed Metadata Site Column in SharePoint Online
Here is how to add a managed metadata field with PnP PowerShell:
#Parameter
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Create a Managed Metadata column from "Regions" term set in "Deals Pipeline" Group
Add-PnPTaxonomyField -DisplayName "Region" -InternalName "Region" -TermSetPath "Deals Pipeline|Regions"
Wrapping up
In conclusion, creating managed metadata site columns in SharePoint is an effective way to store and manage metadata in a consistent and controlled manner. Using PowerShell to create these site columns is a fast and efficient method that helps to streamline the process and avoid manual errors.