0

I am not able to delete the list item using the getitembyid(ID) method using SP Online REST call. I am making a call to deleteitem(murl) where murl ===>

   for (var i = 0; i < data.d.results.length; 
       i++) 
        {  
         var item = data.d.results[i];  
             ID = item.ID;
       ID = item.ID
      var murl =  
  _spPageContextInfo.webAbsoluteUrl + 
 "/_api/web/lists/getByTitle('Posts_Testing')
   /getItemById("+ID+")";
.....
     }

Below is the code. What am I doing wrong?

  function deleteItem(murl) {
  alert(' inside delete fun');
    alert(murl);
    $.ajax({
   url: _spPageContextInfo.webAbsoluteUrl + 
   murl,
     type: "DELETE",
    headers: {
   "accept": 
   "application/json;odata=verbose",
  "X-RequestDigest": 
   $("#__REQUESTDIGEST").val(),
    "If-Match": "*"
       },
     success: function (data) {
     },
        error: function (error) {
       alert(JSON.stringify(error));
        }
     });
   }
          return true;
    }
</script>

I am getting error:

ErrorinRESTAPI-delete-call

1

It looks like your call is wrong.

type should be Post

and there should be a property for "X-HTTP-Method": "DELETE"

Overall ajax call should be something like this:

$.ajax({
    url: <endpoint for item>,
    type: "POST",
    contentType: "application/json;odata=verbose",
    headers: {
        "Accept": "application/json;odata=verbose",
        "X-ReuqestDigest": $("#__REQUESTDIGEST").val(),
        "IF-MATCH": "*",
        "X-HTTP-Method": "DELETE"
    },
    success: function(data){
        console.log('success');
    },
    error: function(data){
        console.error('error');
    }
});

Check out this post CRUD Operations in SharePoint Using REST API - Delete Operations

1

Maybe it is better to use recycle() method, because later you can recover deleted items from recycle bin.

function DeleteListItem (listName, itemId) {
    return $.ajax({
        url: encodeURI(_spPageContextInfo.webServerRelativeUrl + "/_api/Web/lists/getbytitle('" + listName + "')/items(" + itemId + ")/recycle()"),
        type: 'POST',
        headers: {
            "accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "X-HTTP-Method": "DELETE",
            "If-Match": "*"
        }
    });
}

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.