0
CAMLQuery: "<Query><Where><And><Eq><FieldRef Name='Item' />
<Value Type='Lookup'>" + itemid + "</Value></Eq><Eq>
<FieldRef Name='Author' /><Value Type='Integer'>
<UserID /></Value></Eq></And></Where><OrderBy>
<FieldRef Name='ID' Ascending='FALSE' /></OrderBy>
<RowLimit>1</RowLimit></Query>",

My issue is that it is delete multiple rows and not just one

All help appreicated

Corbin Spicer
  • 285
  • 1
  • 8
  • 26
  • This is just the query and doesn't actually delete anything. What are you calling to delete the item? – kei Mar 24 '14 at 21:32
  • This is a snippet within SP Services. It does delete all items that match the where, but I wanted to just delete the one – Corbin Spicer Mar 25 '14 at 07:37

2 Answers2

0

In the end I had to do quite a horrible workaround:

function DeleteItem(itemid) {

     $().SPServices({
     operation: "GetListItems",
     async: false,
 webURL: "MYURL",
     listName: "Basket",
     CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='Item' />    <FieldRef Name='Item:Title' /></ViewFields>",
     CAMLQuery: "<Query><Where><And><Eq><FieldRef Name='Item' /><Value Type='Lookup'>" + itemid + "</Value></Eq><Eq><FieldRef Name='Author' /><Value Type='Integer'><UserID /></Value></Eq></And></Where><OrderBy><FieldRef Name='ID' Ascending='FALSE' /></OrderBy></Query>",
completefunc: function (xData, Status) {

  $(xData.responseXML).SPFilterNode("z:row").each(function() {
    alert($(this).attr("ows_ID"));
 $().SPServices.SPUpdateMultipleListItems({
    listName: "Basket",
    webURL: "MYURL",
    CAMLRowLimit: 1,
    CAMLQuery: "<Query><Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>" + $(this).attr("ows_ID") + "</Value></Eq></Where></Query>",
    batchCmd: "Delete",
    completefunc: function(xData, Status) {


      CountItems();
      ViewBasket();
  CreateSuccess('Item deleted');

 if (event.preventDefault) { event.preventDefault(); } else { event.returnValue = false; } // Stop the form acting like it normally would (refreshing the page)

    }});


return false;
  });
  }
  });




return true;

}   

Basically I had to do a get all items lookup, look through each one, delete it on the ID and break the loop after the first iteration. Horrible workaround but couldn't think of another way to do it.

Corbin Spicer
  • 285
  • 1
  • 8
  • 26
0

Your question is not clear. What are you trying to achieve? GetListItems is to get the list items, not to delete. So if you want to delete items with a WHERE clause you have to call GetListItems first, and then call UpdateListItems in providing the ID of the items you want to delete. Is your "itemit" passed to DeleteItems will return only ONE record from the Basket list?

Your code should look like that :

function DeleteItem(itemid) {
  $().SPServices({
    operation: "GetListItems",
    async: true, /* don't use FALSE ! */
    webURL: "MYURL",
    listName: "Basket",
    CAMLViewFields: "<ViewFields><FieldRef Name='ID' /><FieldRef Name='Title' /><FieldRef Name='Item' /><FieldRef Name='Item:Title' /></ViewFields>",
    CAMLQuery: "<Query><Where><And><Eq><FieldRef Name='Item' /><Value Type='Lookup'>" + itemid + "</Value></Eq><Eq><FieldRef Name='Author' /><Value Type='Integer'><UserID /></Value></Eq></And></Where><OrderBy><FieldRef Name='ID' Ascending='FALSE' /></OrderBy></Query>",
    completefunc: function (xData, Status) {
      // use `.eq(0)` to look at the first one only
      $(xData.responseXML).SPFilterNode("z:row").eq(0).each(function() {
        alert($(this).attr("ows_ID"));
        var ID = $(this).attr("ows_ID");
        // now delete it with `UpdateListItems`
        $().SPServices({
          operation: "UpdateListItems",
          webURL: "MYURL",
          listName: "Basket",
          updates: '<Batch OnError="Continue" ListVersion="1"  ViewName=""><Method ID="1" Cmd="Delete"><Field Name='ID'>"+ID+"</Field></Method></Batch>',
          completefunc: function (xData, Status) {
            alert("OK")
          }
        });
      })
    }
  })
}

FYI I've created a library that is easier to use: http://aymkdn.github.io/SharepointPlus/

For your code it will look like :

function DeleteItem(itemid) {
  // here I suppose that `itemid` will return only one record
  $SP().list("Basket").remove({
    where:"Item = '"+itemid+"'", 
    success:function() { alert("Done!") }
  })
}
AymKdn
  • 3,327
  • 23
  • 27