0

I'm developing a SharePoint App where I need to delete a number of photos that the user will select, so I have a function that I pass an array with the URLs of the photos that will be deleted, but I don't know why it isn't working. This is the function I'm using!

function deleteFiles(arrayURLs) {
    var siteUrl = _spPageContextInfo.siteAbsoluteUrl;
    var webUrl = _spPageContextInfo.webAbsoluteUrl
    var scriptbase = siteUrl + "/_layouts/15/";
    $.getScript(scriptbase + "SP.RequestExecutor.js", ejecutarCrossDomain());
    function ejecutarCrossDomain() {
        var executor = new SP.RequestExecutor(webUrl);
        for (var i = 0, len = arrayURLs.length; i < len; i++) {
            var urlRest = webUrl + "/_api/sp.appcontextsite(@target)/web/getfilebyserverrelativeurl('" + arrayURLs[i] + "')?@target='" + siteUrl + "'";
            console.log(urlRest)
            executor.executeAsync({
                url: urlRest,
                type: "POST",
                headers: {
                    "accept": "application/json;odata=verbose",
                    "content-type": "application/json;odata=verbose",
                    "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                    "X-HTTP-Method": "DELETE",
                    "IF-MATCH": "*"
                },
                success: succes,
                error: function (error) {
                    alert(JSON.stringify(error));
                    loadingOff();
                }
            });
        }
    }
    function succes() {
        console.log('Deleted');
    }
}

When I try it, it seems like it work, logging the Deleted in the console too. Thanks to the one that can help me!!

1 Answer 1

8

Verify the array and the URLs. I modified your script using something similar the following direct API requests so I could do a quick test, and I got your result. My error was an incorrect URL to the files.

Here's a pure AJAX and REST delete that works just fine:

$.ajax(
{
    "url": "/sites/test/_api/web/getfilebyserverrelativeurl('/sites/test/Shared%20Documents/AirplaneLogo.jpg')",
    "method": "POST",
    "headers": {
        "accept": "application/json;odata=verbose",
        "content-type": "application/json;odata=verbose",
        "X-HTTP-Method": "DELETE",
        "If-Match": "*",
        "X-RequestDigest": "yourRequestDigestGoesHere"
    },
    "success" = successfunction,
    "error" = errorfunction
}
);

You can send the items to the Recycle Bin by adding "/recycle()" to the API URL.

A successful Delete returns no data. A successful recycle() returns the GUID for the recycled item.

0

Your Answer

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

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