SharePoint Online: Add Managed Metadata Column to List using PowerShell
Requirement: Create a managed metadata column in SharePoint Online.
SharePoint Online: How to Add Managed Metadata Field to List or Library
Managed Metadata in SharePoint uses the Term set from the term store as the source for its allowed values. A term set defines a set of values. E.g., Department is a Term set, and “Sales” is a Term. So, when the term set “Department” is used as the source for a Managed Metadata column, the term “Sales” can be selected as its value. Here is how to add a managed metadata column to the SharePoint Online list.
- Browse to your SharePoint Online site and navigate to the target list where you want to add the Managed Metadata column.
- Under the List tab, click on the “Create Column” button in the ribbon.
- Provide the Name to your new column, specify the field type as “Managed Metadata”
- Scroll down and select the appropriate Term set or Term from the taxonomy store.
- Fill in other optional values and click “OK” to create a Managed Metadata field in the SharePoint Online list.
PowerShell to Create Managed Metadata column in SharePoint Online List:
Using PowerShell, let’s add a managed metadata column to the SharePoint Online list.
#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 column to list
Function Add-ManagedMetadataColumnToList()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $ListName,
[Parameter(Mandatory=$true)] [string] $Name,
[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] $TermGroupName,
[Parameter(Mandatory=$true)] [string] $TermSetName
)
#Generate new GUID for Field ID
$FieldID = New-Guid
Try {
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get the web, List and Lookup list
$Web = $Ctx.web
$List = $Web.Lists.GetByTitle($ListName)
$LookupList = $Web.Lists.GetByTitle($LookupListName)
$Ctx.Load($Web)
$Ctx.Load($List)
$Ctx.Load($LookupList)
$Ctx.ExecuteQuery()
#Check if the column exists in list already
$Fields = $List.Fields
$Ctx.Load($Fields)
$Ctx.executeQuery()
$NewField = $Fields | where { ($_.Internalname -eq $Name) -or ($_.Title -eq $DisplayName) }
if($NewField -ne $NULL)
{
Write-host "Column $Name already exists in the List!" -f Yellow
}
else
{
#Get the Term set data
$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()
#Create Managed Metadata Column
$FieldSchema = "<Field Type='TaxonomyFieldType' ID='{$FieldID}' DisplayName='$DisplayName' Name='$Name' Description='$Description' Required='$IsRequired' EnforceUniqueValues='$EnforceUniqueValues' />"
$NewField = $List.Fields.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
$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 "New Column Added to the List Successfully!" -ForegroundColor Green
}
}
Catch {
write-host -f Red "Error Adding Column to List!" $_.Exception.Message
}
}
#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$Name="ProjectRegions"
$DisplayName="Project Regions"
$Description="Select the Project Region"
$TermGroupName="Regions"
$TermSetName="MENA"
#Call the function to add column to list
Add-ManagedMetadataColumnToList -SiteURL $SiteURL -ListName $ListName -Name $Name -DisplayName $DisplayName -Description $Description -TermGroupName $TermGroupName -TermSetName $TermSetName
PnP PowerShell to Add Managed Metadata Column in SharePoint Online
Here is the PnP PowerShell to create a new MMS column in the SharePoint Online list using the Add-PnPTaxonomyField cmdlet.
#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$ListName = "Projects"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Add a Managed Metadata column from "Regions" term set in "Deals Pipeline" Group
Add-PnPTaxonomyField -DisplayName "Region" -InternalName "Region" -TermSetPath "Deals Pipeline|Regions" -List $ListName
Hey, thanks for sharing all this knowledge :D. Is there anyway to add Managed Metadata fields in SPO scriots for sitedesigns templates??