Forum Discussion
PHubaut
Feb 17, 2021Copper Contributor
Set-PnPListItem - Multiple Managed Metadata Field
I need to update a Multivalued Managed Metadata field in a document library (sometimes resetting value as well), I'd like to use Set-PnPListItem to do this. Assuming $LibraryName and $Id are respect...
- Feb 17, 2021
PHubaut did not try it, nor am I a super-efficient PowerShell developer, but could you try to:
-create a main array that will contain all your items
-for each object, add to the array. Not sure of you need to create an array with @(yourVal) inside the foreach for the object and append to the main array like: $mainArray = $mainArray + $itemArray?
Sorry of this does not help
James Love
Sep 27, 2022Brass Contributor
PHubaut I just had the exact same thing as you - but I managed to get the "inline" method (with the Foreach-Object) working when I called .ToString() on my term.
$properties = @{}
foreach ($propertyName in $metadataFields) {
$value = $targetFileItem[$propertyName]
if ($value.GetType().Name -eq "TaxonomyFieldValue") {
$value = $value.TermGuid
}
if ($value.GetType().Name -eq "TaxonomyFieldValueCollection") {
$termArray = @()
$value | Select-Object -ExpandProperty TermGuid | ForEach-Object { $termArray = $termArray + $_.ToString()}
$value = $termArray
}
$properties.Add($propertyName, $value)
}
PHubaut
Sep 27, 2022Copper Contributor
Hi James Love, thanks for the trick. I do not have the environment required to test this but trust it is indeed a good solution. Thanks for the update as this scenario could happen to me (and to others too) in the future.