SharePoint Online: Update Managed Metadata Field Value using PowerShell and CSOM

Requirement: Update managed metadata column value using PowerShell in SharePoint Online.

SharePoint Online: PowerShell-CSOM script to update managed metadata Field Value
Assuming the data is already populated in Term Store, Here is the PowerShell script to set value for managed metadata column.
#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"
 
#Set parameter values
$SiteURL="https://crescent.sharepoint.com/sites/Ops/"
$ListName="Projects"
$ItemID="1"
$TaxonomyFieldTitle="Region"

#Termset parameters
$TermGroupName="Regions"
$TermSetName="MENA"
$TermName="UAE"

Try { 
        #Get Credentials to connect
        $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 list and Item
        $List=$Ctx.web.Lists.GetByTitle($ListName)
        $Item = $List.GetItemById($ItemID)
        $Ctx.Load($Item)
        $Ctx.ExecuteQuery()

        #Get the Taxonomy Session
        $TaxonomySession = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($Ctx);
        $TaxonomySession.UpdateCache()
        $TermStore =$TaxonomySession.GetDefaultSiteCollectionTermStore()
        $Ctx.Load($TaxonomySession)
        $Ctx.Load($TermStore)
        $Ctx.ExecuteQuery()

        #Get the Term Group    
        $TermGroup = $TermStore.Groups.GetByName($TermGroupName)
        $Ctx.Load($TermGroup)

        #Get the termset
        $TermSet = $TermGroup.TermSets.GetByName($TermSetName)
        $Ctx.Load($TermSet)
        
        #Get the term
        $Term = $TermSet.Terms.GetByName($TermName) 
        $Ctx.Load($Term)
        $Ctx.ExecuteQuery()
       
        #sharepoint online powershell update managed metadata
        $Item[$TaxonomyFieldTitle] = $Term.Id.ToString()
        $Item.Update()
        $Ctx.ExecuteQuery()

        Write-host -f Green "Taxonomy Column Value has been updated successfully!"
     }
    Catch {
        write-host -f Red "Error updating Taxonomy Field Value!" $_.Exception.Message
   }
This script updates the given managed metadata column value to the specified term.
SharePoint Online Update Managed Metadata Field Value using PowerShell CSOM
In Short, You can update MMS Field with Term GUID! E.g: $ListItem["ManagedMetadataField"] = "6165a8c3-1e46-45de-abfe-2dc28dd1f8c6"

SharePoint Online: PowerShell to Update Managed Metadata
Alternatively, you can cast the column and set the MMS field value too. Here is an example (Replace from Line # 52):
#Get the Field
$Field = $List.Fields.GetByInternalNameOrTitle($TaxonomyFieldTitle)
$Ctx.Load($Field)
$Ctx.ExecuteQuery()

#Cast to Taxonomy Field
$TaxField = [Microsoft.SharePoint.Client.ClientContext].GetMethod("CastTo").MakeGenericMethod([Microsoft.SharePoint.Client.Taxonomy.TaxonomyField]).Invoke($Ctx, $Field)
$TaxFieldValue = New-Object Microsoft.SharePoint.Client.Taxonomy.TaxonomyFieldValue 
$TaxFieldValue.TermGuid = $Term.Id

#sharepoint online powershell to set managed metadata field value
$TaxField.SetFieldValueByValue($Item,$TaxFieldValue)
$Item.Update()
$Ctx.ExecuteQuery()

Update Multi-Valued Managed Metadata column using PowerShell in SharePoint Online: 
The above script(s) sets a managed metadata field with single value. How about updating a MMS column which has "Allow Multiple Values" setting enabled? 
#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"
 
#Set parameter values
$SiteURL="https://crescent.sharepoint.com/sites/Ops/"
$ListName="Projects"
$ItemID="1"
$TaxonomyFieldTitle="Region"

#Managed Metadata parameters
$TermGroupName="Regions"
$TermSetName="MENA"
$TermNames = @('UAE','Qatar')

Try { 
        #Get Credentials to connect
        $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 list and Item
        $List=$Ctx.web.Lists.GetByTitle($ListName)
        $Item = $List.GetItemById($ItemID)
        $Ctx.Load($Item)
        $Ctx.ExecuteQuery()

        #Get the Taxonomy Session
        $TaxonomySession = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($Ctx);
        $TaxonomySession.UpdateCache()
        $TermStore =$TaxonomySession.GetDefaultSiteCollectionTermStore()
        $Ctx.Load($TaxonomySession)
        $Ctx.Load($TermStore)
        $Ctx.ExecuteQuery()

        #Get the Term Group    
        $TermGroup = $TermStore.Groups.GetByName($TermGroupName)
        $Ctx.Load($TermGroup)

        #Get the termset
        $TermSet = $TermGroup.TermSets.GetByName($TermSetName)
        $Ctx.Load($TermSet)
        
        #Get All terms from the termset
        $Terms = $TermSet.GetAllTerms()
        $Ctx.Load($Terms)
        $Ctx.ExecuteQuery()

        $TermValues = @();
        #Get Each Term from the given Terms Array and form the Term String. E.g. "-1;#UAE|b3816a66-12d4-4232-f99e4620a;#-1;#Qatar|casae2c4-195-42d5-gb73ebcd5"
        ForEach($Term in $Terms)
        {
            ForEach($TermName in $TermNames)
            {
                If($TermName -eq $Term.Name)
                {
                    $TermValues += "-1;#" + $Term.Name + "|" + $Term.Id
                    $TermValuesString = $TermValues -join ";#"
                }
            }
        }

        #Get the Field to update
        $Field = $List.Fields.GetByInternalNameOrTitle($TaxonomyFieldTitle)
        $Ctx.Load($Field)
        $Ctx.ExecuteQuery()

        #Cast to Taxonomy Field
        $TaxField = [Microsoft.SharePoint.Client.ClientContext].GetMethod("CastTo").MakeGenericMethod([Microsoft.SharePoint.Client.Taxonomy.TaxonomyField]).Invoke($Ctx, $Field)

        #update managed metadata column powershell sharepoint online
        $TaxFieldValues = New-Object Microsoft.SharePoint.Client.Taxonomy.TaxonomyFieldValueCollection($Ctx, $TermValuesString, $TaxField)
        $TaxField.SetFieldValueByValueCollection($Item, $TaxFieldValues)
        $Item.Update()
        $Ctx.ExecuteQuery()

        Write-host -f Green "Taxonomy Column Value has been updated successfully!"
     }
    Catch {
        write-host -f Red "Error updating Taxonomy Field Value!" $_.Exception.Message
   }

PnP PowerShell to Update Managed Metadata Column in SharePoint Online
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com/sites/marketing"
$ListName = "Projects"
$ItemID = 1

#Set MMS Column value in: "Group|Termset|Parent|Child" format
$FieldValue = "Deals Pipeline|Regions|MENA|UAE"

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

#Update Managed Metadata Column value using PowerShell
Set-PnPListItem -List $ListName -Identity $ItemID -Values @{"Region"= $FieldValue}
SharePoint Online: Update Managed Metadata Field Value using PowerShell and CSOM SharePoint Online: Update Managed Metadata Field Value using PowerShell and CSOM Reviewed by Salaudeen Rajack on August 28, 2017 Rating: 5

1 comment:

  1. Hi There,

    This is great script - I would like to thank you for putting effort to help your fellow consultants :)

    I was wondering if there you could enhance this script to update all items within a document library and get Column values from a CSV file map with Item ID?

    Thanks once again.
    Regards

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.