0

I'm getting this error when i'm trying to execute delete code

SCRIPT5007: Unable to get property 'deleteObject' of undefined or null reference

Here is my code...Not sure what is going wrong here.

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 deleteListItems(){
    var listCount = listitems.get_count();
    for(var i =listCount; i>=0; i--){
        var itemToDel = listitems.itemAt(i);
        itemToDel.deleteObject();
    }    }, function deleteQF(sender, args) {
alert("Request Failed. \nError: " + args.get_message());});}
1

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());});}
  • i want to see the value that's passing in list count. how do i do that? – SpM May 13 '16 at 16:20
  • Sorry, I do not understand your question? You just need to change the part above in your for-loop declaration – Robert Lindgren May 13 '16 at 16:40
  • Noworries, i had the alert pop up with itemcount value. i changed the code to for(var i=listCount-1; i>=0;i--).... But the issue is, items are not deleted in first button click..i had to click it 3 times to get the items deleted. – SpM May 13 '16 at 16:42
  • And i still see this error when i hit F12. – SpM May 13 '16 at 16:48
  • After the for-loop has finished you will need to do a new executeQueryAsync call to actually send the request to SharePoint to delete the items – Robert Lindgren May 13 '16 at 19:48

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.