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

1 Answer 1

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));
    }
});
1
  • 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. Commented Mar 22, 2018 at 14:46

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.