1
votes

For example I have a list call "shopping list", it has 2 columns. 1st Column Type is People or Groups(Allows Multiple People), the Column Name is "Customers". 2nd Column Type is string.

I can use ajax to post information to the normal SharePoint List, but just not to this Shopping List. I know how to format the data for single user column, but not for multiple users column, The single user column is something like: The column name is Customer, the data format should be:

item.CustomerID=1;

I have tried lots of way to format the data, but it is still the same. The server return 500 without any useful information. Example formats below:

item.Customers = [1,2,3]   //1,2,3 is the user id in SharePoint.
item.CustomersID=[1,2,3]  //1,2,3 is the user id in SharePoint.
item.Customers=[{UserId:1},{UserId:2},{UserId:3}]

I can confirm that the javascript object has been stringify and the post request is correctly sent as I successfully added new items to the same list without filling in the Customers Column.

Can someone please advise me what is the correct format to post content to SharePoint 2010 REST API with multiple user columns.

1

1 Answers

2
votes

In case of SharePoint 2010 REST Interface the proper payload for multi-valued user field should look like below:

'<MultiUserFieldName>': [
    { 
        __metadata: { 
            "uri": "<WebUrl>/_vti_bin/listdata.svc/UserInformationList(<UserId>)"
        }
    }
]

where

  • MultiUserFieldName - user field name
  • WebUrl - absolute web url
  • UserId - user Id

JavaScript example

var webUrl = "http://contoso.intranet.com";
var listName = "ShoppingCart";
var properties = {
    'Title': 'Item #123',
    'Customers': createPayloadForMultiUserField(webUrl,[1,2,3])
};


createListItem(webUrl,listName,properties)
.done(function(data){
    console.log('Item ' + data.d.Title + ' has been created'); 
})
.fail(function(error){
    console.log(JSON.stringify(error));
});

where

function createListItem(webUrl,listName, itemProperties) {
    return $.ajax({
        url: webUrl + "/_vti_bin/listdata.svc/" + listName,
        type: "POST",
        processData: false,
        contentType: "application/json;odata=verbose",
        data: JSON.stringify(itemProperties),
        headers: {
            "Accept": "application/json;odata=verbose"
        }
    });
}


function createPayloadForMultiUserField(webUrl,userIds)
{
    return userIds.map(function(id){
        return { 
            __metadata: { 
                "uri": webUrl + "/_vti_bin/listdata.svc/UserInformationList(" + id + ")"
            }
        } 
    });
}