none
SP2010 Client Object Model: How to assign a value to a PeoplePicker field?

    Question

  • How do I assign a value to a PeoplePicker field with the Client Object Model?  Is it as simple as assigning it a string that is the user's AD account?, e.g:

    ListItem item = client.Web.Lists.GetByTitle("My List");
    client.Load(item);
    client.ExecuteQuery();
    
    item["My People Picker"] = "mydomain\\myaccount";
    item.Update();
    client.ExecuteQuery();

    What if I want to assign multiple values?

    Thanks!

    Wednesday, February 29, 2012 2:37 AM

Answers

  • User urs = web.EnsureUser(string.Format("{0}\\{1}", DomainName, userName));

    clientContext.Load(theUser);

    clientContext.ExecuteQuery();

    item["My People Picker"] = usr;

    item.Update();

    clientContext.ExecuteQuery();

    I think for multiple values, you need to enable the Allow Multiple selections on the field first.


    - Sid

    • Proposed as answer by Varun Malhotra Wednesday, February 29, 2012 4:39 AM
    • Marked as answer by TruckStuff Thursday, March 1, 2012 11:54 AM
    Wednesday, February 29, 2012 3:31 AM
  • Hi TruchStuff,

    If the user field is multiple values enabled, you can use the following code to update its value:


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.SharePoint.Client;
    namespace UpdateMultipleUsers
    {
        class Program
        {
            static void Main(string[] args)
            {
                ClientContext clientContext = new ClientContext("http://sps2010-01");
                List oList = clientContext.Web.Lists.GetByTitle("PeoplePickerList");
                ListItem item = oList.GetItemById(1);
                clientContext.Load(item);
                clientContext.ExecuteQuery();
                //Initiate a FieldUserValue array. In this sample, it will have 2 FieldUserValues in it.
                FieldUserValue[] users = new FieldUserValue[2];
                //Get the first user
                User user = clientContext.Web.EnsureUser("contoso\\client1");
                clientContext.Load(user);
                clientContext.ExecuteQuery();
                //Add the user to the first element of the FieldUserValue array.
                users[0] = new FieldUserValue();
                users[0].LookupId = user.Id;
                //Get the second user
                user = clientContext.Web.EnsureUser("contoso\\client2");
                clientContext.Load(user);
                clientContext.ExecuteQuery();
                //Add the user to the second element of the FieldUserValue array.
                users[1] = new FieldUserValue();
                users[1].LookupId = user.Id;
                //Update the field's value.
                item["PPColumn"] = users;
                item.Update();
                clientContext.ExecuteQuery();
            } 
        }
    }

    Thanks,
    Jinchun Chen


    Jinchun Chen
    Forum Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff AT microsoft.com(Please replace AT with @)

    • Marked as answer by TruckStuff Thursday, March 1, 2012 11:54 AM
    Thursday, March 1, 2012 3:34 AM
    Moderator

All replies

  • User urs = web.EnsureUser(string.Format("{0}\\{1}", DomainName, userName));

    clientContext.Load(theUser);

    clientContext.ExecuteQuery();

    item["My People Picker"] = usr;

    item.Update();

    clientContext.ExecuteQuery();

    I think for multiple values, you need to enable the Allow Multiple selections on the field first.


    - Sid

    • Proposed as answer by Varun Malhotra Wednesday, February 29, 2012 4:39 AM
    • Marked as answer by TruckStuff Thursday, March 1, 2012 11:54 AM
    Wednesday, February 29, 2012 3:31 AM
  • Hi TruchStuff,

    If the user field is multiple values enabled, you can use the following code to update its value:


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.SharePoint.Client;
    namespace UpdateMultipleUsers
    {
        class Program
        {
            static void Main(string[] args)
            {
                ClientContext clientContext = new ClientContext("http://sps2010-01");
                List oList = clientContext.Web.Lists.GetByTitle("PeoplePickerList");
                ListItem item = oList.GetItemById(1);
                clientContext.Load(item);
                clientContext.ExecuteQuery();
                //Initiate a FieldUserValue array. In this sample, it will have 2 FieldUserValues in it.
                FieldUserValue[] users = new FieldUserValue[2];
                //Get the first user
                User user = clientContext.Web.EnsureUser("contoso\\client1");
                clientContext.Load(user);
                clientContext.ExecuteQuery();
                //Add the user to the first element of the FieldUserValue array.
                users[0] = new FieldUserValue();
                users[0].LookupId = user.Id;
                //Get the second user
                user = clientContext.Web.EnsureUser("contoso\\client2");
                clientContext.Load(user);
                clientContext.ExecuteQuery();
                //Add the user to the second element of the FieldUserValue array.
                users[1] = new FieldUserValue();
                users[1].LookupId = user.Id;
                //Update the field's value.
                item["PPColumn"] = users;
                item.Update();
                clientContext.ExecuteQuery();
            } 
        }
    }

    Thanks,
    Jinchun Chen


    Jinchun Chen
    Forum Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff AT microsoft.com(Please replace AT with @)

    • Marked as answer by TruckStuff Thursday, March 1, 2012 11:54 AM
    Thursday, March 1, 2012 3:34 AM
    Moderator
  • Jinchun-

    Awesome, that's the part I was still trying to figure out.

    Thanks!

    Thursday, March 1, 2012 11:54 AM
  • Jinchun Chen.

    Thank you. This is just what I was looking for.

    Tuesday, March 27, 2012 8:44 PM
  • What would be the Javascript equivalent code?


    7929

    Wednesday, April 4, 2012 8:49 PM