1

How do I create a new list item for a list that has a user lookup field? I have seen other people pass the ID of the user, but that isn't working for me. I get a 400 error when I try that.

I have added Id to the end of the field name as shown below, which other threads say is required.

Here's what I have been trying -

    var data = {
    __metadata: { 'type': 'SP.Data.ProjectsListItem' },
    Title: '' + name + '',
    Start_x0020_Date: new Date(start).toISOString(),
    End_x0020_Date: new Date(end).toISOString(),
    Project_x0020_ManagerId: 441,
    Additional_x0020_Details: note
    };
    $.ajax({
        url: siteURL + "/_api/web/lists/getbytitle('Projects')/items",
        method: "POST",
        contentType: 'application/json;odata=verbose',
        data: JSON.stringify(data),
        headers: {
            "Accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },
        success: function(data) {
            alert('Item added successfully');
        },
        error: function(error) {
            alert(JSON.stringify(data));
        }
    });
0

You have to set Lookups and User Fields using a results object. If you have a Multi-User Field you should set the value like:

Project_x0020_ManagerId: {"results":[441,448,481]},

If you have a Single-User Field you shet set value like:

Project_x0020_ManagerId: {"results":441}

In your case this code should do the trick:

var data = {
__metadata: { 'type': 'SP.Data.ProjectsListItem' },
Title: '' + name + '',
Start_x0020_Date: new Date(start).toISOString(),
End_x0020_Date: new Date(end).toISOString(),
Project_x0020_ManagerId: {"results":441},
Additional_x0020_Details: note
};
$.ajax({
    url: siteURL + "/_api/web/lists/getbytitle('Projects')/items",
    method: "POST",
    contentType: 'application/json;odata=verbose',
    data: JSON.stringify(data),
    headers: {
        "Accept": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val()
    },
    success: function(data) {
        alert('Item added successfully');
    },
    error: function(error) {
        alert(JSON.stringify(data));
    }
});
  • Thanks for the response. Unfortuantely this still results in a 400 (Bad Request) error, which is so odd. It's a single-user field so I tried Project_x0020_ManagerId: {"results":441} and Project_x0020_ManagerId: {"results":[441]} - neither worked. I also thought maybe the space was causing an issue so I deleted this field and created a new one called ProjectManager and tried writing to ProjectManagerId: {"results":441} and ProjectManagerId: {"results":[441]} and those result in 400 errors too. – Chris Mar 22 '18 at 14:46

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.