Get/Set Managed Metadata Column Values using PowerShell
Here is my awesome collection of PowerShell scripts to read and update Managed Metadata field values in SharePoint:
Read Managed Metadata column Value:
PowerShell to get managed metadata field value.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$WebURL="https://portal.crescent.com/"
$ListName="Deals"
$FieldName="Region"
$ItemID=1
#Get Objects
$Web = Get-SPWeb $WebURL
$List= $Web.Lists[$listName]
$Item = $List.GetItembyID($ItemID)
#Get MMS column Value
[Microsoft.SharePoint.Taxonomy.TaxonomyFieldValue]$MMSFieldValue = $item[$FieldName]
write-host $MMSFieldValue.Label
Get Multiple Value MMS field values using PowerShell:
When “Allow Multiple Values” is Selected, here is how we can retrieve the value of it:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$WebURL="https://portal.crescent.com/"
$ListName="Deals"
$FieldName="Region"
$ItemID=2
#Get Objects
$Web = Get-SPWeb $WebURL
$List= $Web.Lists[$listName]
$Item = $List.GetItembyID($ItemID)
#get managed metadata field value powershell
[Microsoft.SharePoint.Taxonomy.TaxonomyFieldValueCollection]$MMSFieldValueColl = $item[$FieldName]
#Concatenate each term in the value collection
$MMSFieldTerms=""
Foreach ($MMSFieldValue in $MMSFieldValueColl)
{
if($MMSFieldValue.label -ne $null)
{
$MMSFieldTerms+=$MMSFieldValue.label+"; "
}
}
write-host $MMSFieldTerms
Update Managed Metadata Column Value using PowerShell:
Here is how to update managed metadata column with PowerShell.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Variables
$WebURL="https://portal.crescent.com"
$ListName="Deals"
$FieldName="Region"
$ItemID=1
#Get the web
$Web = Get-SPWeb $WebURL
#Get the Term from Term store
$TaxonomySession = Get-SPTaxonomySession -Site $web.Site
$TermStore = $TaxonomySession.TermStores["Managed Metadata Service"]
$TermGroup = $TermStore.Groups["Knowledge Portal"]
$TermSet = $TermGroup.TermSets["Regions"]
$Term = $Termset.Terms["America"]
#You can also get the term directly as: $Term = "15;#America|1c58d657-9bd1-4bff-b1b0-74e52eb717dd"
#Use SharePoint Manager
#Get the List and List Item
$List= $Web.Lists[$listName]
$Item = $List.GetItembyID($ItemID)
#set managed metadata field value powershell
$MMSField = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$Item.Fields[$FieldName]
$MMSField.setFieldValue($Item,$Term)
$Item.Update()
Write-host "Managed Metadata Field value updated!"
Update Managed Metadata Column value for Multiple Values
SharePoint 2013 set managed metadata field using PowerShell.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Variables
$WebURL="https://portal.crescent.com"
$ListName="Deals"
$FieldName="Region"
$ItemID=2
#Get the web
$Web = Get-SPWeb $WebURL
#Get the Term store, Group and Term Set
$TaxonomySession = Get-SPTaxonomySession -Site $web.Site
$TermStore = $TaxonomySession.TermStores["Managed Metadata Service"]
$TermGroup = $TermStore.Groups["Knowledge Portal"]
$TermSet = $TermGroup.TermSets["Regions"]
#Get the List, List Item and Field
$List= $Web.Lists[$listName]
$Item = $List.GetItembyID($ItemID)
$MMSField = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$Item.Fields[$FieldName]
#Actual Term Values to update
$TermValuesColl = @("Africa","Asia","Europe")
#Create a Term field value collection
$MMSValueCollection = new-object Microsoft.SharePoint.Taxonomy.TaxonomyFieldValueCollection($MMSField)
#Form each Term
foreach($TermValue in $TermValuesColl)
{
$Term = $Termset.Terms[$TermValue]
$MMSFieldValue = new-object Microsoft.SharePoint.Taxonomy.TaxonomyFieldValue($MMSField)
$MMSFieldValue.TermGuid = $Term.Id
$MMSFieldValue.Label = $Term.Name
$MMSValueCollection.Add($MMSFieldValue)
}
#updating sharepoint managed metadata columns with powershell: Multi-value MMS column
$MMSField.setFieldValue($Item,$MMSValueCollection)
$Item.Update()
Write-host "Managed Metadata Field value updated!"
Can you provide script to read MMS field value for SharePoint Online pls.
Sure, Here you go: Get Managed Metadata Field Value in SharePoint Online using PowerShell
I am getting the below error when I run the code:
new-object : A constructor was not found. Cannot find an appropriate
constructor for type
Microsoft.SharePoint.Taxonomy.TaxonomyFieldValueCollection.
The error is at the below line:
#Create a Term field value collection
$MMSValueCollection = new-object Microsoft.SharePoint.Taxonomy.TaxonomyFieldValueCollection($MMSField)
I had the same error. In my case it was an incorrect field name in $MMSField.