1

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'.

  • 'a' list contains the pdfs, docx.
  • 'b' list contains the pdf links of 'a' list, name and other columns.
  • I have created an other list 'c' where it contains the name of 'b' list and count.

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

3
  • What is the data type of Count column? Are you getting any error after executing updateItems() method? Feb 17, 2021 at 5:05
  • Yes, I get an error as 400 Bad Request when I included item id as you said and also InvalidClientQueryException with a message "Not well formatted JSON stream".
    – Venkat Sai
    Feb 17, 2021 at 5:58
  • You need to pass the valid ID of item which you want to update. Also, in data, you need to pass the JSON (check reference links how to pass the data). Feb 17, 2021 at 6:24

1 Answer 1

2

To update SharePoint list item, you need to pass the item ID to REST endpoint like:

_spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('c')/items(1)"

Where 1 is ID of SharePoint list item.

References:

  1. Working with lists and list items with REST
  2. CRUD Operations On A SharePoint List Using REST API

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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