问题
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
回答1:
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.
回答2:
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!") }
})
}
来源:https://stackoverflow.com/questions/22611352/caml-query-delete-1-item