SharePoint Online: Add Managed Metadata Column to List using PowerShell

SharePoint Online: How to Add Managed Metadata Field to List or Library:
Managed Metadata in SharePoint uses Term set from term store as the source for its allowed values. Term set defines set of values. E.g. Department is a Term set and "Sales" is a Term. So when the term set "Department" is used to 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 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" 
    Add Managed Metadata field to SharePoint Online List using PowerShell
  • Scroll down and select the appropriate Term set or Term from the taxonomy store.
    PowerShell to create managed metadata column in SharePoint Online List
  • 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

PnP PowerShell to Add Managed Metadata Column in SharePoint Online:
Here is the PnP PowerShell to create new MMS column in SharePoint Online list.
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com/sites/marketing"
$ListName = "Projects"

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

#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
SharePoint Online: Add Managed Metadata Column to List using PowerShell SharePoint Online: Add Managed Metadata Column to List using PowerShell Reviewed by Salaudeen Rajack on December 26, 2016 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.