Set-PnPListItem - Multiple User Field

Copper Contributor

Hey Folks,

 

Trying to update a List item via PowerShell, the column is a multiple user field.

The updated users are currently in an array of Email Addresses which is causing an error.

 

Wondering if anyone knows how I could easily convert this to a string or if there's a workaround?

 

Thanks!

10 Replies

Hi Blair,

 

An array of email addresses is definitely supported.

 

I just tested this out passing in an array of email address strings and it worked fine. Here's the exact syntax:

 

$users = "user@contoso.com", "user2@contoso.com"

Connect-PnPOnline -Url https://contoso.sharepoint.com

 

Set-PnPListItem -List "test1" -Identity 1 -Values @{"TestPeopleMulti"=$users}

 

This updated the user-multi field with both users.

 

Hi Thomas,

Yeah I've done it before, my script below is still causing an error:

 

    $salesmangers = $ListItem.SalesManager.Email
    $salesmangers += $newmanager
    $newlistofmanagers = $salesmangers | Where-Object { $_ –ne $currentmanger }
  
    Set-PnPListItem -List $customerlistID -Identity $ListItem.ID -Values @{"SalesManager" = $newlistofmanagers}  

What error are you getting and where is it failing?

 

Also, to access that field, try $salesmangers = $ListItem.FieldValues.SalesManager.Email

Error is: 

Set-PnPListItem : The specified user  could not be found.

 

Line: 

Set-PnPListItem -List $customerlistID -Identity $ListItem.ID -Values @{"SalesManager" = $newlistofmanagers}

 

Swapping to FieldValues.SalesManager.Email made no difference either.

Try checking your $newlistofmanagers before passing it into Set-PnPListItem, and check $newmanager to make sure they look right.

 

Something must be going wrong somewhere along the line of building that array, since an array of valid email addresses will work with that cmdlet no problem.

Checked the array, but looks fine with valid email addresses.

$newlistofmanagers.GetType() also returns as an array as well so not sure what's happening....

Checked the array, but looks fine with valid email addresses.

$newlistofmanagers.GetType() also returns as an array as well so not sure what's happening!

Seems to be " | Where-Object { $_ –ne $currentmanger } " that is causing the error.

 

If I run $newmanagerslist += $salesmanger then the Set-PnPListItem, it works as expected.

Not sure how else to remove an item from an Array as .Remove("") is not supported.

What exactly are you trying to do with the Where-Object? Are you basically looking to remove an email address that matches "$currentmanager" from that array before adding it to the List Item?

 

Try this:

 

$newlistofmanagers = @()
    foreach ($manager in $salesmanagers)
    {
        if ($manager -ne $currentmanager)
        {
      $newlistofmanagers += $manager        
} }

Set-PnPListItem -List $customerlistID -Identity $ListItem.ID -Values @{"SalesManager" = $newlistofmanagers}

 

 

Yeah basically.

What I've used before and worked fine.

Your script below worked perfectly!