SharePoint Online: Create Managed Metadata Site Column using PowerShell

Requirement: Create Managed Metadata Site Column in SharePoint Online using PowerShell

How to Create a Managed Metadata Site Column in SharePoint Online?
Managed Metadata column in SharePoint Online uses Term Set from the term store as the source for its allowed values. Term set defines set of values. E.g. Regions is a Term set and "Asia" is a Term. So when the term set "Regions" is used to 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 SharePoint Online list.
  • Browse to your SharePoint Online site and Navigate to the site settings >> Site Columns
  • Click on "Create" link at the top of the page
  • Provide the Name to your site column, specify the field type as "Managed Metadata"create a managed metadata column sharepoint online
  • Scroll down and select the appropriate Term Set from the term store.
    create managed metadata site column in sharepoint online
  • Fill 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:
#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 = "",
        [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://crescenttech.sharepoint.com"
$ColumnName="ProjectRegion"
$DisplayName="Project Regions"
$ColumnGroup="Crescent Projects"
$TermGroupName="Deals"
$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 new site column "Project Regions" and maps to a term set "Regions" under "Deals" group in the Term store.
SharePoint Online: Create Managed Metadata Site Column using PowerShell SharePoint Online: Create Managed Metadata Site Column using PowerShell Reviewed by Salaudeen Rajack on June 22, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.