5

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?

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);
        }
    });
  • 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 at 6:59

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.