Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm trying to add an SPListItem to an SPList via REST, but have a problem with User or Group and Lookup (information already on this site).

function adddullitem() {

    var item = {
        "__metadata": { "type": "SP.Data.OfficeReqListItem" },
        "Title": "123",
        "SomeUserorGroupField": "1",
        "SomeLookUpField": "10"
    };
    $.ajax({
        url: "http://SITE/_api/web/lists/getByTitle('LISTNAME')/items",
        type: "POST",
        contentType: "application/json;odata=verbose",
        data: JSON.stringify(item),
        headers: {
            "Accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },
        success: function (data) {
            //success(data);
        },
        error: function (data) {
            //failure(data);
        }
    });

}

I'm trying to pass user's ID to User and group field and SPListItem.ID to LookUp field, but it doesn't work.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

It depends whether User/Lookup field value is multiple or not, the following formats are used when specifying field value via SharePoint REST:

  • Single User/Lookup value format: FieldName: LookupIdVal
  • Mutiple User/Lookup value format: FieldName: {"results": [LookupIdVal1,LookupIdVal2] }

How to set multiple lookup/user field value via SharePoint REST API

var listName = 'Tasks';

var itemProperties = {
    '__metadata': { "type": "SP.Data.TasksListItem" },
    "Title": 'New task',
    'AssignedToId': {"results": [12] }  //multi-valued User field value 
};

$.ajax({
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items",
    type: "POST",
    contentType: "application/json;odata=verbose",
    data: JSON.stringify(itemProperties),
    headers: {
        "Accept": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val()
    },
    success: function (data) {
        console.log(JSON.stringify(data.d));
    },
    error: function (data) {
        console.log(data.responseText);
    }
});

How to set single lookup/user field value via SharePoint REST API

var listName = 'Tasks';

var taskProperties = {
    '__metadata': { "type": "SP.Data.TasksListItem" },
    "Title": 'New task',
    'AssignedToId': 12  //single-valued User field value 
};

$.ajax({
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items",
    type: "POST",
    contentType: "application/json;odata=verbose",
    data: JSON.stringify(taskProperties),
    headers: {
        "Accept": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val()
    },
    success: function (data) {
        console.log(JSON.stringify(data.d));
    },
    error: function (data) {
        console.log(data.responseText);
    }
});
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.