SharePoint Online: Update Managed Metadata Field Value using PowerShell
Requirement: Update managed metadata column value using PowerShell in SharePoint Online.
SharePoint Online: PowerShell CSOM script to update managed metadata Field Value
In SharePoint Online, you can set the managed metadata column using PowerShell. This blog post will show you how to update a managed metadata column using PowerShell. We will share some script examples using CSOM and PnP PowerShell to update managed metadata column values.
Assuming the data is already populated in the Term Store, similar to what you see above, Here is the PowerShell script to set the value for the 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: PowerShell to Update Managed Metadata
Alternatively, you can also cast the column and set the MMS field value. 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 a single value. How about updating an MMS column with the “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
Here is how to add value to managed metadata column using PnP PowerShell:
#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$ListName = "Projects"
$ItemID = 1
#Set MMS Column value in: "Group|Termset|Parent|Child" format
$FieldValue = "Crescent|Deals Pipeline|Department|Accounts"
#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 @{"Department"= $FieldValue}
This sets the value of the “Department” MMS column value.
What if you want to set a Managed Metadata column value that allows multiple values with PnP PowerShell? Sure, Just wrap them inside an array!
#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/PMO"
$ListName = "Projects"
$ItemID = 1
#Set Multivalued Managed Metadata Column value in: "Group|Termset|Parent|Child" format
$FieldValue = @("PMO|Classification|IT","PMO|Classification|HR")
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Set Managed Metadata Column value using PowerShell
Set-PnPListItem -List $ListName -Identity $ItemID -Values @{"Department"= $FieldValue}
We can also set the value of a Managed Metadata field using the Set-PnpTaxonomyFieldValue cmdlet. E.g.,
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/PMO"
$ListName = "Projects"
$FieldInternalName = "Department"
$ItemID = 1
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get the List Item
$ListItem = Get-PnPListItem -List $ListName -Id $ItemID
#Set Managed Metadata Column value in: "Group|Termset|Parent|Child" format
Set-PnpTaxonomyFieldValue -ListItem $ListItem -InternalFieldName $FieldInternalName -TermPath 'People|Department|HR'
You can also update the field value using Term ID (GUID) with -TermID parameter. Similarly, to update a managed metadata column with multiple values, use:
#Set Multivalued Managed Metadata Column value in: @{"TermId1"="Label1";"TermId2"="Label2"} format
Set-PnpTaxonomyFieldValue -ListItem $ListItem -InternalFieldName $FieldInternalName -Terms @{"4f31bd74-d1cf-4318-bcbc-6e7b481fcce8"="Engineering";"4038e965-dbb8-4969-8783-02af0cbcbdfd"="HR"}
To get the value of a Managed Metadata column, use: SharePoint Online: PowerShell to get Managed Metadata Field Value
Am I the only one getting an error when casting TaxonomyField in the line #73?
Exception calling “MakeGenericMethod” with “1” argument(s): “GenericArguments[0], ‘Microsoft.SharePoint.Client.Taxonomy.TaxonomyField’, on ‘T CastTo[T](Microso
ft.SharePoint.Client.ClientObject)’ violates the constraint of type ‘T’.”
At line:1 char:1
+ [Microsoft.SharePoint.Client.Taxonomy.TaxonomyField] $taxonomyFld1 = …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentException
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
This may help you: SharePoint Online: Update Metadata Properties from a CSV File using PowerShell