SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 12 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
2  
This has helped me multiple times over the past week :) – jasonscript Apr 23 '15 at 3:04
1  
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 Halani Feb 25 at 11:15
    
You have to add Id to the field name, e.g. AssignedTo becomes AssignedToId – Fergal Apr 25 at 9:13

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');
                    }
                );
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.