SharePoint Online: How to Add Managed Metadata Field to List or Library:
PowerShell to Create Managed Metadata column in SharePoint Online List:
- Browse to your SharePoint Online site and Navigate to the target list in which you want to add Managed Metadata column.
- Under the List tab, click on "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 other optional values and Click on "OK" to create Managed Metadata field in SharePoint Online list.
PowerShell to Create Managed Metadata column in 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="",
[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