Domanda

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

È stato utile?

Soluzione

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top