2

I have a CSV file which contains semicolon-separated Values to a certain field:

Reviewer
domain\userOne;domain\userTWO

In PowerShell (CSOM - executed on my local computer) I am grabbing this data and write it to a list item in SharePoint. The relevant part of the script is:

$Reviewer = $_.Reviewer
$ReviewerTEST = $Reviewer.Split(";")

## Define empty array
$lookupValueCollection = @()

ForEach($ReviewerSolo in $ReviewerTEST){
    Write-Host "$ReviewerSolo"
    $SPUser = $Context.Web.EnsureUser($ReviewerSolo)
    $Context.Load($SPUser)
    $Context.ExecuteQuery()

    If ($SPUser -ne $Null)
    {
        $SPUserValue = New-Object Microsoft.SharePoint.Client.FieldUserValue
        $SPUserValue.LookupId = $SPUser.id
        ##Write-Host "$SPUser.id"
        $lookupValueCollection += $SPUserValue
        Write-Host "$lookupValueCollection"
    }
}

If ($lookupValueCollection.length -gt 0)
{
    $userValueCollection = [Microsoft.SharePoint.Client.FieldUserValue[]]$lookupValueCollection
    $item["Reviewer"] = $userValueCollection
    $item.Update()
    $Context.ExecuteQuery()
}

I have experienced, that only the first person (domain\userOne) is written into the multi-value person field. After adding a third user to the CSV-File I have experienced, that the first and third person is written to the metadata of the item. It generally seems that only every second person is added, so: first, third, fifth, seventh, and so on. I hope someone of you out there can help me :)


Update: 03.12.2019:

$userValueCollection for sure contains both users, because when I use:

$item["Reviewer"] = $userValueCollection[0]

then domain\userOne is written into the SharePoint-field - and when I use:

$item["Reviewer"] = $userValueCollection[1]

then domain\userTwo is written into the SharePoint-field.

It seems that SharePoint can only handle every 2nd person written to the field.

enter image description here

The field is in a document library.

1

Try something like given below (not an exact code like you want. But you can get the idea from it how you can store the values in array and set it):

#User Account to set
$UserAccounts="Crescent\Salaudeen; Crescent\Ravi"
$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()

Reference:

Get-Set Person or Group (People Picker) Field Value using PowerShell in SharePoint.

9
  • Thank you Ganesh :) Will give it a try on monday – SteffPoint Nov 29 '19 at 18:48
  • Afaik, SPFieldUserValueCollection requires the Microsoft.SharePoint.dll. Do you know where I get it from? I am using SharePoint 2013 onprem. (I dont have access to the server, where SharePoint is installed) – SteffPoint Nov 29 '19 at 19:10
  • 1
    Check this and this NuGet package to get the dll for SharePoint 2013 – Ganesh Sanap Nov 30 '19 at 2:35
  • 1
    It may already be on the SharePoint server. – Underverse Nov 30 '19 at 4:24
  • @ Underverse: I unfortunately don't have access to the server. Is there any trick to reference from my CSOM PowerShell script to this file? – SteffPoint Nov 30 '19 at 9:37
1

Your all code seems fine but you have not added "ExecuteQuery" after updating the field value and that's why it's not working!

Try to change your last if statement with below:

If ($lookupValueCollection.length -gt 0)
{
    $userValueCollection = [Microsoft.SharePoint.Client.FieldUserValue[]]$lookupValueCollection
    $item["Reviewer"] = $userValueCollection
    $item.Update()
    $Context.ExecuteQuery()
}

Hope this will help you.

3
  • Hello Dikesh. Thank you very much for your assistance. I have actually had the ExecuteQuery() method later in my script (I am updating more fields). However, I have updated my Code as you said and only tried to update the multi-valued person-field. Still I got the same result - only one person is returned in the SharePoint field. I guess there may be any kind of restriction for document libraries - but I don't know where :( I've heard it from a colleague, that the script works for SharePoint lists, but not for document libraries. Do you have any further ideas what may cause the issue? – SteffPoint Dec 16 '19 at 6:42
  • 1
    I have checked on custom list only. I will check for document library. – Dikesh Gandhi Dec 16 '19 at 6:48
  • Maybe to give some more details to my context: The PowerShell-Script is executed on a Windows 2016 Server - but it's not the server, where SharePoint is running on. I unfortunately don't have access to the SP-Server and that's why I am stuck on CSOM. – SteffPoint Dec 16 '19 at 7:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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