0

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()

1 Answer 1

1

Load SharePoint CSOM assemblies in beginning:

Add-Type -Path (Resolve-Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll")
Add-Type -Path (Resolve-Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll")

And try to initialize $UserCollection as below in your code:

$UserCollection = @()
foreach($UserAccount in $UserAccountsColl)
{    
   #Get the User
    $User=$web.EnsureUser($UserAccount)
    $spuserValue = New-Object Microsoft.SharePoint.Client.FieldUserValue
    $spuserValue.LookupId = $User.id  
    $UserCollection += $spuserValue
}
$userValueCollection = [Microsoft.SharePoint.Client.FieldUserValue[]]$UserCollection 

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

Hope this will help you.

4
  • I tried to initialize $UserCollection and loaded relevant path also still getting the same error @dikesh gandhi
    – Arzoo Soni
    Apr 24, 2019 at 9:12
  • Hello Dikesh. Your added .dlls do not deliver the SPFieldUserValueCollection. Therefore, you would need to add the Microsoft.SharePoint.dll.
    – SteffPoint
    Dec 6, 2019 at 12:34
  • 1
    Thanks @SteffPoint for drawing my attention. I have updated my answer. Dec 6, 2019 at 13:42
  • You're welcome. I have made the experience, that when writing multiple users to a person field (multi = true) in a document library (SP2013 on prem), only the first person is written into the SP- field. Do you have any idea, what could be the issue? My script is pretty much the same as yours - see here: sharepoint.stackexchange.com/questions/272870/…
    – SteffPoint
    Dec 6, 2019 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.