简体   繁体   English

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

[英]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 来删除所有项目,但我面临一些问题:

  • Deleting 5000 items took me a whole hour while testing (I added a GetItems step with a row count of 5000).测试时删除 5000 个项目花了我整整一个小时(我添加了一个 GetItems 步骤,行数为 5000)。
  • Some error caused the Flow to 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:我所做的是如下:

  • I declared an array having all the alphabets.我声明了一个包含所有字母的数组。
  • Did an apply to each to the array.对每个数组进行了应用。
  • Within the loop, I queried the list for items where OrganizationName starts with the particular alphabet在循环中,我查询了 OrganizationName 以特定字母开头的项目列表
  • Added the retrieved items to an array将检索到的项目添加到数组中
  • for each item in the array, deleted it from the list对于数组中的每个项目,将其从列表中删除

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.

相关问题 Powershell使用CSOM API从Sharepoint在线获取列表项 - Powershell get list items from Sharepoint online using CSOM API 如何使用PowerShell从Sharepoint Online列表中获取项目? - How to get items from a Sharepoint Online list using PowerShell? PowerShell 更新脚本 SharePoint 在线列表项不保存所有列值 - PowerShell script to update SharePoint Online list items does not save all column values 尝试将 csv 导入 Sharepoint Online 但项目从未导入到列表 - Trying to import csv into Sharepoint Online but items never import to list 无法使用 Power shell 创建 SharePoint 在线列表项 - Unable to create SharePoint online list items using Power shell 使用 powershell 更新 Sharepoint 所有列表项 - Update Sharepoint All list items using powershell Powershell:使用 REST API 从 Sharepoint 列表中删除项目 - 远程服务器返回错误:(400) 错误请求 - Powershell: Using REST API to Delete Items from a Sharepoint List - The remote server returned an error: (400) Bad Request SharePoint Online Powershell CSOM从自定义模板创建列表 - SharePoint Online Powershell CSOM create list from custom template 在线获取Sharepoint中的所有用户 - Get all users in sharepoint online 如果我一次只能检索N个项目,如何使用CSOM从Sharepoint列表中检索所有项目? - How do I retrieve all items from a Sharepoint list using CSOM if I can only retrieve N items as a time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM