Follow in Twitter & Facebook


Like in Facebook

Powered By Blogger Widgets

Free Download

FREE Tools


Saturday, July 22, 2017

Delete all items from SharePoint online list using Rest API

We can delete all items from SharePoint online list using Rest API. We can add the code into a script editor web part which we can add in a web part page in SharePoint online. The same code will work for SharePoint 2013, SharePoint 2016 and SharePoint online also.

Here is the first function we are getting the items and then we are building the rest api url for deleting an items and passing it to the deleteItem(url) method.

The full code looks like below:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

<input type="button" id="btnDeleteAllItems" value="Delete All Items" />
<script>
$(function () {
    bindButtonClick();
});
function bindButtonClick() {
    $("#btnDeleteAllItems").on("click", function () {
        $.ajax({
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('Announcements')/items",
    type: "GET",
    headers: {
        "accept": "application/json;odata=verbose",
    },
    success: function (data) {
        var items = data.d.results;
 $.each(data.d.results, function (key, value) {    
var id=value.ID;
var url = "/_api/Web/Lists/getByTitle('Announcements')/getItemById("+id+")";
            deleteItem(url);
    });    
    },
    error: function (error) {
        alert(JSON.stringify(error));
    }
});
    });
}

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

Once you save the page and click on the button, it will delete all items from the Announcements list.




0 on: "Delete all items from SharePoint online list using Rest API"