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

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.


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 in SharePoint site. This is how it…

How to set character limit for Multiple Lines of Text column in List

Setting a character limit for Multiple lines of text column in a List is not straight forward. Depending upon the setting of the column, the solution varies.

Plain Text:
When you create Multiple lines of text column with "Plain Text", SharePoint renders this field as "TextArea" HTML element. You can set the character limit by making use of maxlength attribute of TextArea element.

$( document ).ready(function(){

    $("textarea[id*=FieldName]").attr('maxlength','1000');

});

The above script should be injected into NewForm.aspx and EditForm.aspx of SharePoint List. Of course, replace "FieldName" with your field name.

Enhanced Rich Text:
When you create Multiple lines of text column with Enhanced Rich Text, SharePoint renders this field as DIV with contenteditable attribute set. This is where it gets more tricky. If you closely observe, the IDs of the contenteditable DIV and the corresponding Label DIV are somewhat similar.

For this DIV,…