[英]Delete all items from SharePoint Online list
I have a SPO site with a list that contains about 12000 items.我有一个包含大约 12000 个项目的列表的 SPO 站点。
I need to refresh these items everyday from an Excel file.我需要每天从 Excel 文件刷新这些项目。
I have setup a Flow to delete all the items, but I am facing some issues:我已经设置了一个 Flow 来删除所有项目,但我面临一些问题:
Fail
even though all 5000 items were deleted.即使删除了所有 5000 个项目,某些错误也会导致流程Fail
。What would be the best way to delete all the items quickly?快速删除所有项目的最佳方法是什么?
I suggest you use PowerShell to achieve it.我建议你使用 PowerShell 来实现它。
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
$SiteUrl = "https://tenant.sharepoint.com/sites/team"
$UserName="admin@tenant.onmicrosoft.com"
$Password ="xxx"
$ListTitle="CustomList";
#Setup Credentials to connect
$context = new-object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
$list = $context.Web.Lists.GetByTitle($ListTitle)
$context.Load($list)
$context.ExecuteQuery()
$continue = $true
while($continue)
{
Write-Host -NoNewline "." -foregroundcolor black -backgroundcolor yellow
$query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery(100, "ID")
$listItems = $list.GetItems($query)
$context.Load($listItems)
$context.ExecuteQuery()
if ($listItems.Count -gt 0)
{
for ($i = $listItems.Count-1; $i -ge 0; $i--)
{
$listItems[$i].DeleteObject()
}
$context.ExecuteQuery()
}
else
{
$continue = $false;
}
}
Write-Host "All listitems deleted from list." -foregroundcolor black -backgroundcolor green
If you want to run the code everyday, we can create a Windows Task Scheduler and run the PowerShell every day.如果您想每天运行代码,我们可以创建一个 Windows 任务计划程序并每天运行 PowerShell。 Refer to: How to Automate PowerShell Scripts with Task Scheduler请参阅: 如何使用任务计划程序自动化 PowerShell 脚本
In my particular scenario, I had a column with an OrganizationName, with values starting with almost every alphabet.在我的特定场景中,我有一个带有 OrganizationName 的列,其值几乎以每个字母开头。
What I did was as below:我所做的是如下:
With the alphabet array, I was able to work around the threshold limit of 5000 items.使用字母数组,我能够解决 5000 个项目的阈值限制。 Each alphabet would have a maximum of 1500 names.每个字母最多可以有 1500 个名字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.