从 SharePoint Online 列表中删除所有项目

Delete all items from SharePoint Online list

我有一个 SPO 站点,其列表包含大约 12000 个项目。

我需要每天从 Excel 文件中刷新这些项目。

我已经设置了一个流程来删除所有项目,但我遇到了一些问题:

快速删除所有项目的最佳方法是什么?

建议你使用PowerShell来实现

Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll" 
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\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 

如果您想每天 运行 代码,我们可以每天创建一个 Windows 任务计划程序和 运行 PowerShell。参考:How to Automate PowerShell Scripts with Task Scheduler

在我的特定情况下,我有一个包含 OrganizationName 的列,其值几乎以每个字母开头。

我所做的如下:

  • 我声明了一个包含所有字母的数组。
  • 是否将每个应用到数组。
  • 在循环中,我在列表中查询了 OrganizationName 以特定字母表开头的项目
  • 已将检索到的项目添加到数组中
  • 对于数组中的每一项,将其从列表中删除

使用字母表数组,我能够绕过 5000 个项目的阈值限制。每个字母最多有 1500 个名字。