0

I get a success message after running the following code displayed below. However, when I go and verify the list item the file is still there. If I try the client side of this code, the file gets deleted, I'm not sure if I'm missing something.

function initialize(){
    var scriptbase = hostweburl + "/_layouts/15/";     
    $.getScript(scriptbase + "SP.RequestExecutor.js", execCrossDomainRequest); 

  function execCrossDomainRequest() {  
            var executor = new SP.RequestExecutor(appweburl);                           
            var digest = $("#__REQUESTDIGEST").val();    

            executor.executeAsync({              
                url: appweburl + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('TestList')/getItemById(1)/AttachmentFiles/getbyFileName('test - File.xlsx')?@target='" +  hostweburl + "'",    
                type: "POST",
                headers: {
                    "Accept": "application/json;odata=verbose",
                    "X-Http-Method": "DELETE",
                    "X-RequestDigest": $("#__REQUESTDIGEST").val()
                },             
                success: function (data) {  
                    alert("success: " + JSON.stringify(data));  
                },  
                error: function (err) {  
                    alert("error: " + JSON.stringify(err));  
                }    
            });                              
   } 
}
1

What you need to do is to specify type: "DELETE" in your ajax call and also specify the matching of etag (known as entity tag).

This code demonstrates that-

function deleteItem (item) {     
$.ajax({
    url: item.__metadata.uri,
    type: "DELETE",
    contentType: "application/json;odata=verbose",        
    headers: {
        "Accept": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val(),
        "X-HTTP-Method": "DELETE",
        "If-Match": item.__metadata.etag
    },
    success: function (data) {
        console.log("delete Successful");            
    },
    error: function (msg) {
        console.log(msg);
    }
});
}

Also make sure your app has correct permission in appmanifest file.

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