SOLVED

Set-PnPListItem - Multiple Managed Metadata Field

%3CLINGO-SUB%20id%3D%22lingo-sub-2142326%22%20slang%3D%22en-US%22%3ESet-PnPListItem%20-%20Multiple%20Managed%20Metadata%20Field%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2142326%22%20slang%3D%22en-US%22%3E%3CP%3EI%20need%20to%20update%20a%20Multivalued%20Managed%20Metadata%20field%20in%20a%20document%20library%20(sometimes%20resetting%20value%20as%20well)%2C%20I'd%20like%20to%20use%26nbsp%3B%3CA%20title%3D%22Set-PnPListItem%20(PnP.Powershell)%20%7C%20Microsoft%20Docs%22%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Ffr-fr%2Fpowershell%2Fmodule%2Fsharepoint-pnp%2Fset-pnplistitem%3Fview%3Dsharepoint-ps%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ESet-PnPListItem%3C%2FA%3E%20to%20do%20this.%3CBR%20%2F%3EAssuming%20%24LibraryName%20and%20%24Id%20are%20respectively%20the%20document%20library%20name%20and%20an%20Id%20of%20an%20existing%20document%2C%20assuming%20%24mm1%20and%20%24mm2%20are%20two%20identifiers%20of%20terms%20in%20the%20right%20scope%20-%20here%20is%20code%20sample%20to%20illustrate%20the%20issue%20-%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24lib%26nbsp%3B%3D%26nbsp%3B%22BI%22%3B%26nbsp%3B%24id%26nbsp%3B%3D%26nbsp%3B45934%3B%0A%24mm1%26nbsp%3B%3D%26nbsp%3B%22f5d4baae-6d12-42bf-b3f1-76f06ef0f037%22%3B%26nbsp%3B%24mm2%26nbsp%3B%3D%26nbsp%3B%22bca7c6ab-a948-4cd1-b1b6-38d9001b47bc%22%0ASet-PnPListItem%26nbsp%3B-List%26nbsp%3B%24lib%26nbsp%3B-Identity%26nbsp%3B%24Id%26nbsp%3B-Values%26nbsp%3B%40%7B%22Tags%22%26nbsp%3B%3D%26nbsp%3B%24null%7D%0ASet-PnPListItem%26nbsp%3B-List%26nbsp%3B%24lib%26nbsp%3B-Identity%26nbsp%3B%24Id%26nbsp%3B-Values%26nbsp%3B%40%7B%22Tags%22%26nbsp%3B%3D%26nbsp%3B(%24mm1%2C%26nbsp%3B%24mm2)%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CDIV%3E%3CU%3Eworks%20just%20fine%3C%2FU%3E%20and%20so%20is%20the%20following%20code%20too%3A%3C%2FDIV%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%24mml1%20%3D%20%40(%24mm1%2C%20%24mm2)%20%23%20%24mml1%20%3D%20(%22...%22%2C%20%22...%22)%20or%20%20%24mml1%20%3D%20%22...%22%2C%20%22...%22%20work%20too%0ASet-PnPListItem%20-List%20%24lib%20-Identity%20%24Id%20-Values%20%40%7B%22Tags%22%20%3D%20%24mml1%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EHowever%2C%20obviously%2C%20the%20list%20of%20terms'%20GUID%20(an%20array%20of%20strings)%20is%20not%20that%20simple%20and%20is%20build%20somewhere%20else%20and%2C%20to%20illustrate%20the%20issue%20I%20am%20facing%20-%20%3CU%3Ethe%20following%20code%20does%20not%20work%3C%2FU%3E%20for%20me%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%23%20Basically%2C%20this%20academical%20code%20is%20aimed%20to%20create%20%24mml2%2C%20a%20%5BString%5B%5D%5D%20object%0A%23%20equivalent%20to%20%24mml1%20but%20build%20in%20a%20way%20neither%20the%20strings%20nor%20their%20number%20are%0A%23%20known%20in%20advance%0A%24mml2%20%3D%20%24mml1%20%7C%20Foreach-Object%20-Process%20%7B%24_%7D%20%23%20i.e.%20a%20String%20Array%20build%20by%20code%0ASet-PnPListItem%20-List%20%24lib%20-Identity%20%24Id%20-Values%20%40%7B%22Tags%22%20%3D%20%24mml2%7D%20%23%20%26lt%3B%3D%20FAILS!%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20get%20the%20following%20error%20and%20couldn't%20figure%20out%20how%20to%20avoid%20it%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3ESet-PnPListItem%20%3A%20Value%20cannot%20be%20null.%0AParameter%20name%3A%20path%0AAt%20line%3A1%20char%3A1%0A%2B%20Set-PnPListItem%20-List%20%24lib%20-Identity%20%24Id%20-Values%20%40%7B%22Tags%22%20%3D%20%24mml3%7D%20%23%20%20...%0A%2B%20~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%0A%20%20%20%20%2B%20CategoryInfo%20%20%20%20%20%20%20%20%20%20%3A%20WriteError%3A%20(%3A)%20%5BSet-PnPListItem%5D%2C%20ArgumentNullException%0A%20%20%20%20%2B%20FullyQualifiedErrorId%20%3A%20EXCEPTION%2CPnP.PowerShell.Commands.Lists.SetListItem%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20the%20above%20examples%2C%3C%2FP%3E%3CDIV%3E%3CUL%3E%3CLI%3E%3CSPAN%3EBoth%20%24mml1%20and%20%24mml2%20happen%26nbsp%3Bto%26nbsp%3Bbe%26nbsp%3Ball%26nbsp%3BObject%5B%5D%2C%26nbsp%3Bbase%26nbsp%3Btype%3A%26nbsp%3BSystem.Array%3C%2FSPAN%3E%3C%2FLI%3E%3CLI%3E%3CSPAN%3EBoth%20have%26nbsp%3B2%26nbsp%3Bindividual%26nbsp%3Belements%3C%2FSPAN%3E%3C%2FLI%3E%3CLI%3E%3CSPAN%3EIndividual%26nbsp%3Belements%26nbsp%3Bare%26nbsp%3Bof%26nbsp%3Btype%26nbsp%3BString%2C%26nbsp%3Bbase%26nbsp%3Btype%3A%26nbsp%3BSystem.Object%3C%2FSPAN%3E%3C%2FLI%3E%3CLI%3E%3CSPAN%3ETheir%26nbsp%3Brespective%20individual%26nbsp%3Belements%26nbsp%3Ball%26nbsp%3Bmatch%3C%2FSPAN%3E%3C%2FLI%3E%3C%2FUL%3E%3C%2FDIV%3E%3CP%3EWhat%20did%20I%20miss%3F%3CBR%20%2F%3EHow%20should%20I%20build%20my%20array%20of%20terms'%20GUID%20so%20that%20update%20of%20the%20field%20works%20with%20Set-PnPListItem%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2142482%22%20slang%3D%22en-US%22%3ERe%3A%20Set-PnPListItem%20-%20Multiple%20Managed%20Metadata%20Field%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2142482%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F163596%22%20target%3D%22_blank%22%3E%40Pierre%20Hubaut%3C%2FA%3E%26nbsp%3Bdid%20not%20try%20it%2C%20nor%20am%20I%20a%20super-efficient%20PowerShell%20developer%2C%20but%20could%20you%20try%20to%3A%3C%2FP%3E%0A%3CP%3E-create%20a%20main%20array%20that%20will%20contain%20all%20your%20items%3C%2FP%3E%0A%3CP%3E-for%20each%20object%2C%20add%20to%20the%20array.%20Not%20sure%20of%20you%20need%20to%20create%20an%20array%20with%26nbsp%3B%40(yourVal)%20inside%20the%20foreach%20for%20the%20object%20and%20append%20to%20the%20main%20array%20like%3A%20%24mainArray%26nbsp%3B%20%3D%20%24mainArray%20%2B%20%24itemArray%3F%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESorry%20of%20this%20does%20not%20help%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2142614%22%20slang%3D%22en-US%22%3ERe%3A%20Set-PnPListItem%20-%20Multiple%20Managed%20Metadata%20Field%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2142614%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F9907%22%20target%3D%22_blank%22%3E%40Joel%20Rodrigues%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20Joel%20-%20I%20reshaped%20my%20tests%20based%20on%20your%20idea%20-%3CBR%20%2F%3EI%20am%20not%26nbsp%3B%3CSPAN%3Ea%20super-efficient%20PowerShell%20developer%20either%20and%20this%20may%20be%20the%20issue.%3CBR%20%2F%3E%3C%2FSPAN%3E%3CU%3EThis%20works%3C%2FU%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%23%20WORKS%0A%24mainarray1%20%3D%20%40()%0A%24mainarray1%20%3D%20%24mainarray1%20%2B%20%24mm1%0A%24mainarray1%20%3D%20%24mainarray1%20%2B%20%24mm2%0ASet-PnPListItem%20-List%20%24lib%20-Identity%20%24Id%20-Values%20%40%7B%22Tags%22%20%3D%20%24mainarray1%7D%20%23%20WORKS%0A%23%20WORKS%20TOO%0A%24mainarray2%20%3D%20%40()%0Aforeach%20(%24term%20in%20%24mainarray1)%20%7B%0A%20%20%20%20%24mainarray2%20%3D%20%24mainarray2%20%2B%20%24term%0A%7D%0ASet-PnPListItem%20-List%20%24lib%20-Identity%20%24Id%20-Values%20%40%7B%22Tags%22%20%3D%20%24mainarray2%7D%20%23%20WORKS%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CU%3EBut%20this%20does%20not%3C%2FU%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24mainarray3%20%3D%20%40()%0A%24mainarray1%20%7C%20Foreach-Object%20-Process%20%7B%24mainarray3%20%3D%20%24mainarray3%20%2B%20%24_%7D%0ASet-PnPListItem%20-List%20%24lib%20-Identity%20%24Id%20-Values%20%40%7B%22Tags%22%20%3D%20%24mainarray3%7D%20%23%20FAILS%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESo%20the%20error%20is%20related%20to%20the%20%22For%20Each%22-way%20to%20add%20items%20to%20the%20array%20(statement%20or%20cmdlet).%3CBR%20%2F%3EJust%20for%20the%20record%2C%20after%20running%20above%20code%2C%20doing%20a%20%24mainarray2.GetType()%2C%20%24mainarray2%5B0%5D.GetType()%2C%20%24mainarray2%5B1%5D.GetType()%26nbsp%3Band%20same%20for%20%24mainarray3%20reveals%20exact%20same%20data%20types.%26nbsp%3BFurthermore%2C%26nbsp%3B%24mainarray2%5B0%5D.equals(%24mainarray3%5B0%5D)%20and%26nbsp%3B%24mainarray2%5B1%5D.equals(%24mainarray3%5B1%5D)%20both%20returns%20True.%20I%20don't%20see%20the%20difference.%3CBR%20%2F%3E%3CBR%20%2F%3EThanks%20for%20the%20idea%20as%20I%20have%20a%20workaround%20!%3CBR%20%2F%3E...yet%20remaining%20with%20my%20doubts%20about%20what%20is%20going%20wrong.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

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 respectively the document library name and an Id of an existing document, assuming $mm1 and $mm2 are two identifiers of terms in the right scope - here is code sample to illustrate the issue -

 

$lib = "BI"; $id = 45934;
$mm1 = "f5d4baae-6d12-42bf-b3f1-76f06ef0f037"; $mm2 = "bca7c6ab-a948-4cd1-b1b6-38d9001b47bc"
Set-PnPListItem -List $lib -Identity $Id -Values @{"Tags" = $null}
Set-PnPListItem -List $lib -Identity $Id -Values @{"Tags" = ($mm1, $mm2)}

 

works just fine and so is the following code too:

 

$mml1 = @($mm1, $mm2) # $mml1 = ("...", "...") or  $mml1 = "...", "..." work too
Set-PnPListItem -List $lib -Identity $Id -Values @{"Tags" = $mml1}

However, obviously, the list of terms' GUID (an array of strings) is not that simple and is build somewhere else and, to illustrate the issue I am facing - the following code does not work for me:

# Basically, this academical code is aimed to create $mml2, a [String[]] object
# equivalent to $mml1 but build in a way neither the strings nor their number are
# known in advance
$mml2 = $mml1 | Foreach-Object -Process {$_} # i.e. a String Array build by code
Set-PnPListItem -List $lib -Identity $Id -Values @{"Tags" = $mml2} # <= FAILS!

 

I get the following error and couldn't figure out how to avoid it

 

Set-PnPListItem : Value cannot be null.
Parameter name: path
At line:1 char:1
+ Set-PnPListItem -List $lib -Identity $Id -Values @{"Tags" = $mml3} #  ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (:) [Set-PnPListItem], ArgumentNullException
    + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Lists.SetListItem

 

In the above examples,

  • Both $mml1 and $mml2 happen to be all Object[], base type: System.Array
  • Both have 2 individual elements
  • Individual elements are of type String, base type: System.Object
  • Their respective individual elements all match

What did I miss?
How should I build my array of terms' GUID so that update of the field works with Set-PnPListItem?

2 Replies
Best Response confirmed by Pierre Hubaut (Occasional Contributor)
Solution

@Pierre Hubaut 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

 

@Joel Rodrigues 

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.