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
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
- PHubautFeb 17, 2021Copper Contributor
Thanks Joel - I reshaped my tests based on your idea -
I am not a super-efficient PowerShell developer either and this may be the issue.
This works# WORKS $mainarray1 = @() $mainarray1 = $mainarray1 + $mm1 $mainarray1 = $mainarray1 + $mm2 Set-PnPListItem -List $lib -Identity $Id -Values @{"Tags" = $mainarray1} # WORKS # WORKS TOO $mainarray2 = @() foreach ($term in $mainarray1) { $mainarray2 = $mainarray2 + $term } Set-PnPListItem -List $lib -Identity $Id -Values @{"Tags" = $mainarray2} # WORKS
But this does not
$mainarray3 = @() $mainarray1 | Foreach-Object -Process {$mainarray3 = $mainarray3 + $_} Set-PnPListItem -List $lib -Identity $Id -Values @{"Tags" = $mainarray3} # FAILS
So the error is related to the "For Each"-way to add items to the array (statement or cmdlet).
Just for the record, after running above code, doing a $mainarray2.GetType(), $mainarray2[0].GetType(), $mainarray2[1].GetType() and same for $mainarray3 reveals exact same data types. Furthermore, $mainarray2[0].equals($mainarray3[0]) and $mainarray2[1].equals($mainarray3[1]) both returns True. I don't see the difference.
Thanks for the idea as I have a workaround !
...yet remaining with my doubts about what is going wrong.- James LoveSep 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) }
- PHubautSep 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.