kwizcom banner advertisement

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()
       
        #Update the Field value
        $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
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
        $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 the field value
        $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
   }
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

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.