4

I'm currently working on a script to update a SharePoint list in a 2013 Dedicated environment.

One of the fields on the list is a Multi-Valued 'People Picker' field and I can't figure out how to properly set its value using Powershell and the Microsoft.SharePoint.Client namespace.

I have a list of Usernames in the format of 'domain\username'. I'm iterating through each and calling SPWeb.EnsureUser to get a Microsoft.SharePoint.Client.User object.

After that, I'm at a loss as to what I should be doing. Below is a example of what I've been trying. The EnsureUser function is just a helper function I wrote that retrieves the Web object from the current Site $context and then call $web.EnsureUser. Once this is done, however, my list item does not contain the user I would expect.

$userName = "whd\wholland"
$spuser = EnsureUser $context $user             
if($spuser -ne $null){
     #Really not sure what to do here.
     $spuserValue = New-Object Microsoft.SharePoint.Client.FieldUserValue
     $spuserValue.LookupId = $spuser.id                 
     $listItem["AllOwners"] = @($spuserValue)
}
$listItem.Update()

1 Answer 1

7

Finally found the solution to my problem.

I was trying to set the value of my field to a generic array.

What I needed to do was to create a generic array, add my user value to it and then convert the array to have the appropriate specific type...FieldUserValue in this case.

$userName = "whd\wholland"
$spuser = EnsureUser $context $userName
$lookupValueCollection = @()             
if($spuser -ne $null){         
     $spuserValue = New-Object Microsoft.SharePoint.Client.FieldUserValue
     $spuserValue.LookupId = $spuser.id                 
     $lookupValueCollection += $spuserValue
}
$userValueCollection = [Microsoft.SharePoint.Client.FieldUserValue[]]$lookupValueCollection 
$listItem["AllOwners"] = $userValueCollection
$listItem.Update()
2
  • Is that a cast instead of a convert on the line where you set $userValueCollection? Also, shouldn't you be setting $listItem["AllOwners"] = $userValueCollection? Your code here shows setting it to $mvLookup which isn't set in the snippet you provided. Commented Sep 25, 2015 at 18:18
  • 1
    Yes. I'm casting the array as a specific type. 'Convert' was just the semantics I used to describe it. Edited answer to correct the $userValueCollection typo - What I get for trying to scrub my example after a copy/paste.
    – WCHolland
    Commented Sep 25, 2015 at 18:25

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.