I've been working with this problem from one month but unable to solve this. I'm able to retrieve the list using GET method rest api. But I'm not able to update the items in the list using POST method in rest api. For clear picture, our idea is to get number of clicks count clicked on a link and it should update the count automatically in the list for that link name. So, previous developers had created two lists already named 'a' and 'b'.
So, I am trying to update the count column in 'c' list. But am unable to update the count from the code itself. The code below is one of the methods I tried. Please help me resolve this problem where am clueless even after trying many methods from sharepoint blogs and tutors from last month.
function getItems() {
var resturl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('c')/items";
$.ajax({
url: resturl,
type: "GET",
headers: {
"accept": "application/json;odata=verbose",
},
success: function(data) {
//console.log(data.d.results);
var items = data.d.results;
for (var i = 0; i < items.length; i++) {
console.log("From C List -> Name: " + items[i].Title + ", Count: " + items[i].Count);
}
},
error: function(error) {
alert(JSON.stringify(error));
}
});
}
var prevcount = 0;
function updateItems() {
var resturl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('c')/items";
prevcount = prevcount + 1;
$.ajax({
url: resturl,
method: "POST",
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"content-Type": "application/json;odata=verbose",
"IF-MATCH": "*",
"X-HTTPS-Method": "MERGE"
},
data: "{__metadata:{'type':'SP.Data.cListItem'}, Count: prevcount}",
async: false, success: function (data) {
console.log(data.d.results);
},
error: function (data) {
console.log(data.responseJSON.error);
}
});
}
In your updateItems
function, you need to specify the list item id to update in the resturl
, like this:
var resturl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('c')/items(1)";
You could refer to this article for more: https://www.codesharepoint.com/rest-api/update-listitem-in-sharepoint-using-rest-api