19

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.

Robert Lindgren
  • 24,520
  • 12
  • 53
  • 79
Chetvergov
  • 613
  • 1
  • 9
  • 20

2 Answers2

28

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);
    }
});
Vadim Gremyachev
  • 42,498
  • 3
  • 86
  • 167
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');
                    }
                );
Amay Kulkarni
  • 631
  • 8
  • 10
  • Needs the code for GetCurrentUser() to be posted. Assuming it returns the current user's ID, and that the field is Participant, using ParticipantId to fill it is correct. Post could've benefited from that additional explanation. – vapcguy Aug 14 '19 at 18:11