Skip to main content
added 1219 characters in body
Source Link
Robert Lindgren
  • 24.5k
  • 12
  • 53
  • 79

You need to start the iteration on listCount-1 because the array is zero-based.

Also, after the loop has finished you need to do a new executeQueryAsync to actually save / send the changes to the server.

So complete example:

var context = SP.ClientContext.get_current();
 var web = context.get_web();
     var oList;
    var listitems;
   function deletelistItems() {
      var oList = web.get_lists().getByTitle('TracingList');
    var query = new SP.CamlQuery();
query.set_viewXml("<Where><Contains><FieldRef Name='Recepient' /><Value Type='Text'>Recepient Created!</Value></Contains></Where>");

listitems = oList.getItems(query);
context.load(web);
context.load(listitems);
context.executeQueryAsync(function (){
    var listCount = listitems.get_count();
    for(var i =listCount -1; i>=0; i--){
        var itemToDel = listitems.itemAt(i);
        itemToDel.deleteObject();
    }
    
    context.executeQueryAsync(function(){ 
        alert("Success"); 
    }, function(sender, args) {
        alert("Request Failed. \nError: " + args.get_message());
    });   
}, function (sender, args) {
alert("Request Failed. \nError: " + args.get_message());});}

You need to start the iteration on listCount-1 because the array is zero-based.

You need to start the iteration on listCount-1 because the array is zero-based.

Also, after the loop has finished you need to do a new executeQueryAsync to actually save / send the changes to the server.

So complete example:

var context = SP.ClientContext.get_current();
 var web = context.get_web();
     var oList;
    var listitems;
   function deletelistItems() {
      var oList = web.get_lists().getByTitle('TracingList');
    var query = new SP.CamlQuery();
query.set_viewXml("<Where><Contains><FieldRef Name='Recepient' /><Value Type='Text'>Recepient Created!</Value></Contains></Where>");

listitems = oList.getItems(query);
context.load(web);
context.load(listitems);
context.executeQueryAsync(function (){
    var listCount = listitems.get_count();
    for(var i =listCount -1; i>=0; i--){
        var itemToDel = listitems.itemAt(i);
        itemToDel.deleteObject();
    }
    
    context.executeQueryAsync(function(){ 
        alert("Success"); 
    }, function(sender, args) {
        alert("Request Failed. \nError: " + args.get_message());
    });   
}, function (sender, args) {
alert("Request Failed. \nError: " + args.get_message());});}
Source Link
Robert Lindgren
  • 24.5k
  • 12
  • 53
  • 79

You need to start the iteration on listCount-1 because the array is zero-based.