1

I just wanted update a particular field not the entire row of the list, is there any way to do it by using the rest API.

0

You can use this function, with only setting that one field in the json.

// Update item
// item = { __metadata: {type: SP.Data.xxxListItem }, ....} 
UpdateListItem = function (listName, itemId, item) {
    return jQuery.ajax({
        url: encodeURI(_spPageContextInfo.webServerRelativeUrl + "/_api/web/lists/getbytitle('" + listName + "')/items(" + itemId + ")"),
        type: "POST",
        data: JSON.stringify(item),
        headers: {
            "Accept": "application/json; odata=verbose",
            "Content-Type": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "X-HTTP-Method": "MERGE",
            "If-Match": "*"
        }
    });
}

So:

var item =  { __metadata: {type: SP.Data.MyListNameListItem }, FieldToChange: "test"}
UpdateListItem("MyListName", 10, item) 
| improve this answer | |
0

In RestApi there is method called Merge which take your current data and merge i.e update it with new data. Below function helps to solve your question.

function UpdateListItem(listName,myID)
    {

        var valToUpdate = "abcde";              

        var item = {
            "__metadata": { "type": itemType },
            "listItem": valToUpdate,

        };

        $.ajax({
            url:  _spPageContextInfo.webAbsoluteUrl  + "/_api/web/lists/getbytitle('" + listName + "')/items(" + myID + ")",
            type: "POST",
            contentType: "application/json;odata=verbose",
            data: JSON.stringify(item),
            headers: {
                "Accept": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                "IF-MATCH": "*",
                "X-HTTP-Method":"MERGE",
            },
            success: function (data) {
                alert("success");
            },
            error: function (data) {
                alert("failed");
            }
        });
    }

"X-HTTP-Method":"MERGE" it will update our new value to the old value later.Finally, as our task here is to update data in SharePoint entity (list in this case), the operation is of type/method – “POST”. The success block alerts a “success” string, after which a page refresh takes place, and the updated data can be seen in the list item.

| improve this answer | |

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.