18

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.

26

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);
    }
});
  • 2
    This has helped me multiple times over the past week :) – jasonscript Apr 23 '15 at 3:04
  • 2
    i was try it but not work for me..can you please explain more. i am pass Id of user in json post but not work its give 400 error – Vishal Thakkar Feb 25 '16 at 11:15
  • 2
    You have to add Id to the field name, e.g. AssignedTo becomes AssignedToId – Fergal Apr 25 '16 at 9:13
  • It would be very helpful if the fact you have to add Id to the field name was present at the beginning of the answer – netadictos Jul 18 '17 at 10:16
0

yes add Id to column names it worked:

Columns :

Course_x0020_Id and Participant

    var courseId = GetQueryStringValues('ID');
    var currentUser  = GetCurrentUser();
    var todaysDate = GetTodaysDate();

    var itemToCreate = {'Category' : 'Register', 'Course_x0020_IdId' : courseId, 'ParticipantId' : currentUser, 'RegistrationDate' : todaysDate};
            var listURL = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('listname')/items";
            srvcQuerySPList.createItem(listURL ,itemToCreate)
                .then(
                    function(data){

                        alert("Done");
                    },
                    function(data){
                        alert('Error');
                    }
                );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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