javascript SharePoint 2013 ClientContext:如何通过 MULTIPLE CONDITION 过滤器删除特定列表项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31452356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SharePoint 2013 ClientContext : How to DELETE specific list items by MULTIPLE CONDITION filter?
提问by u775856
By using SP.ClientContext
from Javascript end, below is the code i used to "UPDATE" a list item. Simply:
通过SP.ClientContext
从 Javascript 端使用,下面是我用来“更新”列表项的代码。简单地:
var clientContext = new SP.ClientContext( siteURL );
spList = clientContext.get_web().get_lists().getByTitle( myListName );
this.spList_ExistingItem = spList.getItemById( itemID );
spList_ExistingItem.set_item( 'FullName', myFullName );
spList_ExistingItem.set_item( 'Age', myAge );
spList_ExistingItem.update();
clientContext.executeQueryAsync(succeeded_handler, fail_handler);
This allows me to update
an list item by querying it by ONE condition which is: getItemById(itemID)
here.
这允许我通过一个条件查询update
列表项,即:getItemById(itemID)
这里。
Now let's say i want to delete any item which is:
现在,让我们说,我要删除的项目是:
- Age = 30
- Country = US
- 年龄 = 30
- 国家 = 美国
Then how do i do such query with multiple conditions. And then even to DELETEplease?
那么我如何使用多个条件进行此类查询。然后甚至删除好吗?
UPDATED
更新
According to the answer below, i found the REST API is more easier and cleaner to use for Client/Javascript end, compared to CSOM. (So then, of course i changed all my codes to the REST API way already.)
根据下面的答案,我发现与 CSOM 相比,REST API 用于客户端/Javascript 端更容易、更干净。(那么,当然,我已经将所有代码更改为 REST API 方式。)
So the conclusion is, i suggest to use REST API rather than CSOM (SP.ClientContext).
所以结论是,我建议使用 REST API 而不是 CSOM (SP.ClientContext)。
Thanks! :)
谢谢!:)
回答by Daniel B
This can be done in two different ways, either by using CSOM/JSOM or via the SharePoint REST API. Since you are using the CSOM/JSOM model in your question, I'll only show you how it's done using that method.
这可以通过两种不同的方式完成,使用 CSOM/JSOM 或通过 SharePoint REST API。由于您在问题中使用了 CSOM/JSOM 模型,因此我只会向您展示如何使用该方法完成它。
Using CSOM/JSOM
使用 CSOM/JSOM
To filter SP.ListItem
's on mulitple conditions, there are no single methods that take the arguments as multiple filter fields. Instead, you'll have to resort to using a CAML query to specify the list items you want, as below.
要SP.ListItem
在多个条件下过滤's,没有单一方法将参数作为多个过滤器字段。相反,您将不得不求助于使用 CAML 查询来指定所需的列表项,如下所示。
var clientContext = new SP.ClientContext( siteURL );
spList = clientContext.get_web().get_lists().getByTitle( myListName );
//Create a CAML-query with your filter conditions
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query><Where><And><Eq><FieldRef Name=\'Age\'/>' +
'<Value Type=\'Number\'>30</Value></Eq>
<Eq><FieldRef Name=\'Country\'/>' +
'<Value Type=\'Text\'>US</Value></Eq></And></Where></Query><RowLimit>10</RowLimit></View>');
//The query will return a collection of items matching your conditions
this.collListItem = spList.getItems(camlQuery);
clientContext.load(collListItem);
//Execute the query
clientContext.executeQueryAsync(function () {
var itemCount = collListItem.get_count();
//For each list item in the collection, mark it to be deleted
for (var i = itemCount - 1; i >= 0; i--) {
var oListItem = collListItem.itemAt(i);
oListItem.deleteObject();
};
//Execute the delete operation
clientContext.executeQueryAsync(deleteSucceeded, deleteFailed);
}, fail_handler);
Using SharePoint REST API
使用 SharePoint REST API
This method assumes that you use jQuery to be able to do some simple $.ajax()
calls and use the promise functionality, since you might have multiple items to delete. It also assumes that you understands how you can use jquery deferred objects to chain asynchronous functions to run in sequence.
此方法假定您使用 jQuery 能够执行一些简单的$.ajax()
调用并使用 promise 功能,因为您可能有多个项目要删除。它还假设您了解如何使用 jquery 延迟对象链接异步函数以按顺序运行。
The simple idea is to
简单的想法是
- Make a request to the REST api and get all the items matching your filter conditions
- For each object in the collection returned, make another request that deletes the item, and add the request to an array of requests
- When all requests are done, do whatever you want!
- 向 REST api 发出请求并获取与您的过滤条件匹配的所有项目
- 对于返回的集合中的每个对象,发出另一个删除该项目的请求,并将该请求添加到请求数组中
- 当所有请求完成后,做任何你想做的事!
Note that you might have to modify the REST api call to match your columns. Just use the browser or Postman to check that your request is correct.
请注意,您可能需要修改 REST api 调用以匹配您的列。只需使用浏览器或邮递员检查您的请求是否正确。
function getItemsToDelete () {
//You might have to modify this so it filters correctly on your columns
var requestUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle(" + myListName + ")/items?$filter=Age eq 30 and Country eq 'US'")
//Return and ajax request (promise)
return $.ajax({
url: requestUrl,
type: "GET",
headers: {
"accept": "application/json;odata=verbose",
},
success: function(result) {
$.each(result.d.results, function(index, item){
//Note that we push the ajax-request to the array
//that has been declared a bit down
itemsToDelete.push(deleteItem(item));
});
},
error: function(error) {
//Something went wrong when retrieving the list items
}
});
}
function deleteItem (item) {
//All SP.ListItems holds metadata that can be accessed in the '__metadata' attribute
var requestUrl = item.__metadata.uri;
return $.ajax({
url: requestUrl,
type: "POST",
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"IF-MATCH": item.__metadata.etag,
"X-HTTP-Method": "DELETE"
},
success: function() {
console.log("Item with ID " + item.__metadata.id + " successfully deleted!");
},
error: function(error) {
//Something went wrong when trying to delete the item
}
});
}
//Declare an array of deferred objects that hold a delete request
//for each item that is to be deleted
var itemsToDelete = [];
//First get the items to delete
$.when(getItemsToDelete()).then(function () {
$.when.apply($, itemsToDelete).then(function(){
console.log("All items are deleted!");
});
});
Some useful sources
一些有用的资源
jQuery Deferred objectCRUD operations on list items with SharePoint REST api