Can't Delete item from list

Feb 8, 2011 at 5:32 PM

Good Afternoon.  I'm using jquery 1.3.2 and SPServices 0.5.8 and can't seem to delete a list item.  Here is the code I'm trying to use:

$().SPServices({
   operation: "UpdateListItems",
   async: true,
   listName: "ShoppingCart",
   batchCmd: "Delete",
   CAMLQuery: "<Query><Where><And><Eq><FieldRef Name='Key' /><Value Type='Text'>abc</Value></Eq><Eq><FieldRef Name='Title' /><Value Type='Text'>123</Value></Eq></And></Where></Query>",
   completefunc: function (xData, Status) {
    alert(Status);
   }  
  }
  );

The alert message "success" is displayed but the record is not deleted from the list.  I do have an item in the ShoppingCart list with Key=abc and Title=123.  Now, if I use the ID like this:

  $().SPServices({
   operation: "UpdateListItems",
   async: true,
   listName: "ShoppingCart",
   batchCmd: "Delete",
   ID: 62,
   completefunc: function (xData, Status) {
    alert(Status);
   }  
  }
  );

.. the record is deleted.  Could I be using an out of date jquery file? 

Thank you for any help you can provide :)

Feb 8, 2011 at 6:49 PM

Actually I am not sure if you can just supply a CAML query in that way. I believe that Marc introduced a newer function for updating multiple items that could work. I have been successful just using a query to loop the items to get the id and then deleting them one at a time.

Feb 9, 2011 at 11:21 AM

I agree it isn't possible with the query in the example you provided.
A way to do it is to use a query to get the id's and delete this one. Just as spevilgenius suggested.
Here is an example of how you can do this:

        $().SPServices({
            operation: "GetListItems",
            async: true,
            debug: true,
            listName: "TestList",
            CAMLViewFields: "<ViewFields></ViewFields>",
            CAMLQuery: "<Query><Where><And><Eq><FieldRef Name='Key' /><Value Type='Text'>abc</Value></Eq><Eq><FieldRef Name='Title' /><Value Type='Text'>123</Value></Eq></And></Where></Query>",
            completefunc: function (xData, Status) {
                $(xData.responseXML).find("[nodeName=z:row]").each(function () {
                    $().SPServices({
                        operation: "UpdateListItems",
                        async: true,
                        debug: true,
                        batchCmd: "Delete",
                        listName: "ShoppingCart",
                        ID: $(this).attr("ows_ID"),
                        completefunc: function (xData, Status) {
                            alert(Status);
                        }
                    });
                });
            }
        });
    });

Regards, Anita

Feb 9, 2011 at 12:11 PM

Thank you Anita, that did the trick perfectly!!! 

I hope one day we can use CAML to delete records. 

Thanks again for the help.  I really appreciate it :)

Coordinator
Feb 9, 2011 at 1:49 PM
Edited Feb 9, 2011 at 4:44 PM

Hey, guys. A few things:

  • Yes, I created a function in v0.5.8 to help with this: http://spservices.codeplex.com/wikipage?title=%24%28%29.SPServices.SPUpdateMultipleListItems
  • jQuery 1.3.2 is pretty ancient at this point; I'd suggest upgrading to 1.4.4. Note that the current version of jQuery is 1.5 and SPServices WILL NOT work with this version; I have a fix in v0.6.0, though, which I will release soon.
  • The fix for 1.5 is that there HAVE to be single quotes around things like z:row in this example: .find("[nodeName='z:row']") We're all going to have to get used to that syntax tightening!

M.

Feb 9, 2011 at 1:55 PM
Edited Feb 9, 2011 at 4:56 PM

Thank you Marc.  I'll upgrade my jQuery library and try that example on the page you reference. 

$().SPServices.SPUpdateMultipleListItems({
  listName: "States",
  CAMLQuery: "<Query><Where><Eq><FieldRef Name='Status'/><Value Type='Text'>Inactive</Value></Eq></Where></Query>",
  batchCmd: "Delete"
});

This seems very intuitive and exactly what I'm looking for.

Update: The above code works great!  Thanks again!!!