Skip to main content
edited tags
Link
Dikesh Gandhi
  • 7.2k
  • 4
  • 31
  • 58
Source Link

How to add multiple users in Multi-user People Picker field (Person or Group Field)

I want to add multiple users in multi-picker person field in SharePoint list. I tried below code but always getting an error that:

New-object : Cannot find type [Microsoft.SharePoint.SPFieldUserValueCollection]: verify that the assembly containing this type is loaded.

Can anyone help me to fix this issue?

$ClientContext = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Securedpw = ConvertTo-SecureString $Password -AsPlainText -Force
$ClientContext.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $Securedpw)
$web = $ClientContext.Web 
$list= $web.lists.GetByTitle($listName)
$ClientContext.Load($list)
$ClientContext.ExecuteQuery()
$ListItemCreationInformation = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
$ListItem= $list.AddItem($ListItemCreationInformation)

$UserAccounts="domain\arzoo; domain\ashwin"
$UserAccountsColl = $UserAccounts -split ';'
$UserCollection = new-object Microsoft.SharePoint.SPFieldUserValueCollection
foreach($UserAccount in $UserAccountsColl)
{    
   #Get the User
    $User=$web.EnsureUser($UserAccount)

    #Add to collection
    $UserFieldValue = new-object Microsoft.SharePoint.SPFieldUserValue($Web, $User.ID, $User.LoginName)
    $UserCollection.Add($UserFieldValue)
}

#update the Multiple value Person or Group field
$ListItem[$FieldName] = $UserCollection
$ListItem.Update()