6

I am trying to update a list item using rest api

$.ajax({
                    url: _spPageContextInfo.webAbsoluteUrl + url,
                    type: "PATCH",
                    headers: {
                        "accept": "application/json;odata=verbose",
                        "__metadata":    {"type": "SP.Data.CaseListItem"},
                        "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                        "content-Type": "application/json;odata=verbose",
                        "X-Http-Method": "PATCH",
                        "If-Match": olditem.__metadata.etag
                    },
                    data: JSON.stringify(item),
                    success: function (data) {
                        console.log(data);
                    },
                    error: function (error) {
                        console.log(JSON.stringify(error));
                    }
                });

It throws the below error

{"readyState":4,"responseText":"{\"error\":{\"code\":\"-1, Microsoft.SharePoint.Client.InvalidClientQueryException\",\"message\":{\"lang\":\"en-US\",\"value\":\"A type named 'SP.Data.ClientsListItem' could not be resolved by the model. When a model is available, each type name must resolve to a valid type.\"}}}","responseJSON":{"error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"A type named 'SP.Data.ClientsListItem' could not be resolved by the model. When a model is available, each type name must resolve to a valid type."}}},"status":400,"statusText":"Bad Request"}

I have passed the modelname in metadata of header. What should I change?

3 Answers 3

2

Try it as below:

var item = {
        "__metadata": { "type": "SP.Data.CaseListItem" },
        "Title": "updated title"
    };


    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('CaseList')/items(1)",
        type: "POST",
        contentType: "application/json;odata=verbose",
        data: JSON.stringify(item),
        headers: {
            "Accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "X-HTTP-Method": "MERGE",
            "If-Match": "*"
        },
        success: function (data) {
            console.log(data + " success in updating item");
        },
        error: function (data) {
            console.log(data);
        }
    });
1
  • 1
    Hi Gautam, I'm getting the same error. What was the thing you changed in your post compared to the original post?
    – naijacoder
    Apr 9, 2019 at 6:59
0

The first letter of the SharePoint list when passed into item should be capital letter.

In this case the C in the name is capital at "SP.Data.CaseListItem".

var item = {
    "__metadata": { "type": "SP.Data.CaseListItem" },
    "Title": "updated title"
};
0

You can check error details, if you are getting this "A type named 'SP.Data.ResourcesListItem' could not be resolved by the model. When a model is available, each type name must resolve to a valid type."

Here you need to verify "ListItemEntityTypeFullName" name for your list and include it in your code. You can get details using below :

GET weburl + "/_api/lists/getbytitle('Resources')?$select=ListItemEntityTypeFullName"

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