Skip to main content

How to update Person field with multiple values using REST API

Person or Group field in SharePoint is similar to a Lookup field. When you are updating this field using REST API, you need to append "Id" to the name of the column in the body construct. For example, the body construct looks like this:

data: { "__metadata": { "type": "SP.Data.ListNameListItem" }, "Title": "First Item", "PeopleFieldId": "4" };

The highlighted portions should be replaced by the actual List Name and Column Name. In the above example, the REST call is updating a List item with Title and People columns.

How to get the value for user ID ("4" in the above example) needs a separate explanation and that will be my next post!

The above example works fine if Person field is configured to accept only one value. If we change the Person field to accept multiple values, how do we pass more than one value in the REST call? Since we normally separate user names with semicolon in people picker, I tried separating the values similarly in the above construct something like this:

data: { "__metadata": { "type": "SP.Data.ListNameListItem" }, "Title": "First Item", "PeopleFieldId": "4; 7" };

Code gracefully threw an error as
An unexpected 'PrimitiveValue' node was found when reading from the JSON reader. A 'StartObject' node was expected

When I looked at JSON response from "GET" request, things became clearer.

If Person field is set to accept only one value, I can fetch the value of Person field with the below object hierarchy:

response.d.results[0].PeopleFieldId

If Person field is set to accept more than one value, it changes to something like this:

response.d.results[0].PeopleFieldId.results[0], results[0] will have ID of first user, and results[1] will have ID of second user, and so on.

Notice that, in the case of multiple users, it stores as a collection within the column. So when we are forming the JSON, we need to form the values as a subset. This is how it is changed now.

data: { "__metadata": { "type": "SP.Data.ListNameListItem" }, "Title": "Second Item", "PeopleFieldId": { "results": ["4", "7"] } };

This time, item was created successfully with two users in the column.

Comments

  1. Hey, please let us know if you got chance to write a post of how you got 4 in above example?

    I have similar situation where I need to get specific users ID and pass it while creating item through REST

    Cheers

    ReplyDelete
    Replies
    1. @salah saleh, you can get the user id from _spPageContextInfo.userId property. This will return the user ID of the logged in user.

      Delete
  2. Just to make it more clearer: (I am sure it will save someone sometime)

    var requestApprover = {
    '__metadata' : { 'type': 'Collection(Edm.Int32)' },
    'results': [10,20,30]
    };

    Remember the field name should contain Id at the end and the type should be the field type that you are updating where for people or groups (multiple) is Collection(Edm.Int32).

    ReplyDelete
    Replies
    1. Wow, it saved my time, many thx, why novbody ellse mentioning that fieldname should end with ID????

      Delete
  3. Not sure how you get this working I am getting below mention error

    {
    "status": 400,
    "message": "Invalid JSON. An unexpected comma was found in scope 'Property'. A comma is only valid between properties of an object or between elements of an array.\r\nclientRequestId: df784485-ac72-4880-ac45-8b1716e3c498\r\nserviceRequestId: 2b72839e-0080-6000-28a8-52736ddc3bdb",
    "source": "https://xxxxx.sharepoint.com/xxxx/_api/web/lists/GetByTitle('Product%20Transition%20Tracker')/items(6)",
    "errors": [
    "-1",
    "Microsoft.SharePoint.Client.InvalidClientQueryException"
    ]
    }
    Body is
    {
    '__metadata':{
    'type':'SP.Data.ProductTransitionTrackerListItem'}, 'Title':'Updated',
    'SupplyChainPlannerId':,{ 'results': ['1940', '1636'] },
    'DemandPlannerId':'2573'
    }

    ReplyDelete
  4. Thank you!!!
    Ugh... Microsoft!

    ReplyDelete
  5. Hi, Where is the post to get the UserIDs? I have a requirement. I have a list where in one item field, I have multiple people added. My requirement is to copy this to another list. SO, if 2 people are added into an item, how will get their IDs in the Flow so that I can copy them to a different list

    ReplyDelete
  6. How to add null or no values to People or Group column

    ReplyDelete
    Replies
    1. just set empty []
      Sample code:
      let spItem = new spItem(); // CDRId:number[]=[];

      spItem.CDRId = (formEnteredData.cdReview)? this.getPeoplePickerArr(formEnteredData.cdReview):[];

      private getPeoplePickerArr = (users: IPeoplePickerUser[]): number[] => {
      let spSelectedUsers: number[] =[];

      users.forEach(peoplePickerUser => {
      spSelectedUsers.push(peoplePickerUser.Id);
      });
      return spSelectedUsers;
      }

      //set multivalue peoplepicker
      CDRId': { 'results': spItem.CDRId } ,

      Delete
    2. just set empty []
      Sample code:
      let spItem = new spItem(); // CDRId:number[]=[];

      spItem.CDRId = (formEnteredData.cdReview)? this.getPeoplePickerArr(formEnteredData.cdReview):[];

      private getPeoplePickerArr = (users: IPeoplePickerUser[]): number[] => {
      let spSelectedUsers: number[] =[];

      users.forEach(peoplePickerUser => {
      spSelectedUsers.push(peoplePickerUser.Id);
      });
      return spSelectedUsers;
      }

      //set multivalue peoplepicker
      CDRId': { 'results': spItem.CDRId } ,

      Delete
  7. I have tried with 'results': [0]. But it is giving error

    ReplyDelete

Post a Comment

Popular posts from this blog

All about SharePoint List View Styles

Sometimes, there are out of the box features which we tend to ignore and later when we do apply, we are more than happy about the feature which is readily available in SharePoint. One such feature is List View Style. I never thought I would write a post on this. However, whenever I spoke about this with users, people were excited to see the result. That prompted me to write this post. Instead of getting into only theory part, I will basically take use cases where these styles can be applied and also touch up on on some minor limitations with certain style. When you are creating/modifying a List view, you will get an option to select View Style. As shown below, there are 8 options available and Default is always set if you ignore this style. List of View Styles I will take typical Contact List and Announcement List to explian about these styles. Let us go one by one. Default: This view, as name suggest, is the default style in a view. This is one of the widely seen style

How to get SharePoint List or Library GUID via REST

Sometimes, you would need List or Library GUID to use that in some operation. In such cases, how do you get hold of GUID using REST API? There is a straight-forward end point which you can use: /_api/web/lists/getByTitle('ListTitle')/Id This will return the GUID of a List or Library. There is also another approach. However this approach works only if a List or Library contains at least one item. /_api/web/lists/getByTitle('ListTitle')/items This is a familiar end point which we use to fetch items of a List or Library. To get the GUID from the response, you would do: var listIDTemp = response.data.d.results[0].__metadata.id; var listID = listIDTemp.substring(listIDTemp.lastIndexOf("guid") + 5).split('\'')[0]; As you can see, we are doing string operations to fetch GUID from metadata.id. This contains information only if the response has at least one item.