删除所有列表项目Sharepoint 2013 REST
问题描述:
我需要使用REST API删除Sharepoint列表中的所有项目。
我该如何做到这一点?
我可以删除使用一个单一的项目: “/ _api /网页/列表/ getByTitle( 'MYLIST')/项( 'ID')”删除所有列表项目Sharepoint 2013 REST
我试图删除ID,但没有奏效。
答
您必须使用上面显示的URI对列表中的每个项目进行一次删除调用,并连续传入每个ID。如果列表中有大量项目,则删除然后重新创建列表本身可能会更便宜,速度更快。
答
你可以试试这个
function deleteItem(url) {
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + url,
type: "DELETE",
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"If-Match": "*"
},
success: function (data) {
},
error: function (error) {
alert(JSON.stringify(error));
}
});
}
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('MyList')/items",
type: "GET",
headers: {
"accept": "application/json;odata=verbose",
},
success: function (data) {
var items = data.d.results;
for(var item in items){
var url = "/_api/Web/Lists/getByTitle('MyList')/getItemById(item.ID)"
deleteItem(url);
}
},
error: function (error) {
alert(JSON.stringify(error));
}
});
答
你可以试试这个代码。但是你应该知道,这里可能是你列表中的例外。使用此代码后,我在列表中遇到问题。我删除了所有项目,但我的ListCount属性设置为-3。我建议使用批量请求来形成和执行请求。它会更快更安全
window.I = 0;
deleteFunction();
function deleteListItem(listTitle, listItemId, type)
{
try
{
var listItemUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/items(" + listItemId + ")";
var itemPayload = {'__metadata': {'type': type}};
$.ajax({
url: listItemUri,
type: "POST",
contentType: "application/json;odata=verbose",
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest" : $("#__REQUESTDIGEST").val(),
"X-HTTP-Method": "DELETE",
"If-Match": "*"
},success :function(){
console.log("deleted " + window.I);
window.I++;
deleteFunction();
},
error: function (data) {
window.I++;
deleteFunction();
}
});
}
catch(e)
{
console.log("error" + window.I);
window.I++;
}
}
function deleteFunction()
{
try
{
if(window.I > 1000) return;
deleteListItem('ListName',window.I,'SP.Data.ListNameListItem');
console.log("deleted " + window.I);
}
catch(e)
{
console.log("error" + window.I);
window.I++;
}
}
好的,所以我应该获取列表中的所有项目,并为每个项目调用DELETE。 :(我想我会去删除列表,谢谢 –