图 - 是否有 easier/faster 方法删除 SharePoint 列表中的所有项目?
Graph - Is there easier/faster way to delete all items within a SharePoint list?
以下代码遍历列表并删除每个项目。有什么 "nicer" 方法可以从列表中删除所有项目吗? (除了删除它并重新创建它?)类似 List.Purge
var deleteQueryOptions = new List<QueryOption>()
{
new QueryOption("expand", "fields(select=id)")
};
Console.WriteLine("Deleting ROWS from the list (UPDATING)");
var deleteItems = await graphServiceClient.Sites[siteUrl].Lists[listName].Items
.Request(deleteQueryOptions)
.GetAsync();
do
{
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine("Processing PAGE of LIST ITEMS");
Console.ResetColor();
foreach (var deleteItem in deleteItems)
{
await graphServiceClient.Sites[siteUrl].Lists[listName].Items[deleteItem.Id]
.Request()
.DeleteAsync();
}
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Another PAGE of List Items successfully deleted");
Console.ResetColor();
try
{
deleteItems = await deleteItems.NextPageRequest.GetAsync();
}
catch
{
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine("There is no NextPageRequest for deleting items.");
Console.ResetColor();
}
// While there is another page with data, get it and do the process again (At this moment 1 page contains 200 items, written by Marek Kyzivat @ 05/08/2019 )
} while (deleteItems.NextPageRequest != null);
这样就可以完成工作了,但是一旦你有一个更大的列表,它可能需要一些时间。
如果您像这样更新内部循环,应该会有帮助。
ServicePointManager.DefaultConnectionLimit = 30;
var tasks = new List<Task>();
foreach (var deleteItem in deleteItems)
{
task.Add(graphServiceClient.Sites[siteUrl].Lists[listName].Items[deleteItem.Id]
.Request()
.DeleteAsync());
}
Task.WaitAll(tasks.ToArray());
另请注意,打开连接的默认数量为 2。增加此数量会占用更多内存,但会允许更多并发调用。
我真的很想知道在这种特定情况下对性能的影响。我们目前正在尝试确定更好的编程模型来向 Graph 发出并发请求。
以下代码遍历列表并删除每个项目。有什么 "nicer" 方法可以从列表中删除所有项目吗? (除了删除它并重新创建它?)类似 List.Purge
var deleteQueryOptions = new List<QueryOption>()
{
new QueryOption("expand", "fields(select=id)")
};
Console.WriteLine("Deleting ROWS from the list (UPDATING)");
var deleteItems = await graphServiceClient.Sites[siteUrl].Lists[listName].Items
.Request(deleteQueryOptions)
.GetAsync();
do
{
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine("Processing PAGE of LIST ITEMS");
Console.ResetColor();
foreach (var deleteItem in deleteItems)
{
await graphServiceClient.Sites[siteUrl].Lists[listName].Items[deleteItem.Id]
.Request()
.DeleteAsync();
}
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Another PAGE of List Items successfully deleted");
Console.ResetColor();
try
{
deleteItems = await deleteItems.NextPageRequest.GetAsync();
}
catch
{
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine("There is no NextPageRequest for deleting items.");
Console.ResetColor();
}
// While there is another page with data, get it and do the process again (At this moment 1 page contains 200 items, written by Marek Kyzivat @ 05/08/2019 )
} while (deleteItems.NextPageRequest != null);
这样就可以完成工作了,但是一旦你有一个更大的列表,它可能需要一些时间。
如果您像这样更新内部循环,应该会有帮助。
ServicePointManager.DefaultConnectionLimit = 30;
var tasks = new List<Task>();
foreach (var deleteItem in deleteItems)
{
task.Add(graphServiceClient.Sites[siteUrl].Lists[listName].Items[deleteItem.Id]
.Request()
.DeleteAsync());
}
Task.WaitAll(tasks.ToArray());
另请注意,打开连接的默认数量为 2。增加此数量会占用更多内存,但会允许更多并发调用。
我真的很想知道在这种特定情况下对性能的影响。我们目前正在尝试确定更好的编程模型来向 Graph 发出并发请求。