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

huangapple go评论110阅读模式
英文:

Delete all items from SharePoint Online list

问题

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

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

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

  • 在测试时,删除5000个项目花了我整整一个小时(我添加了一个GetItems步骤,行数为5000)。
  • 一些错误导致Flow“失败”,尽管所有5000个项目都已被删除。

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

英文:

I have a SPO site with a list that contains about 12000 items.

I need to refresh these items everyday from an Excel file.

I have setup a Flow to delete all the items, but I am facing some issues:

  • Deleting 5000 items took me a whole hour while testing (I added a GetItems step with a row count of 5000).
  • Some error caused the Flow to Fail even though all 5000 items were deleted.

What would be the best way to delete all the items quickly?

答案1

得分: 1

I suggest you use PowerShell to achieve it.

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 list items deleted from list." -foregroundcolor black -backgroundcolor green

If you want to run the code every day, we can create a Windows Task Scheduler and run the PowerShell every day. Refer to: How to Automate PowerShell Scripts with Task Scheduler

英文:

I suggest you use PowerShell to achieve it.

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 

If you want to run the code everyday, we can create a Windows Task Scheduler and run the PowerShell every day. Refer to: How to Automate PowerShell Scripts with Task Scheduler

答案2

得分: 0

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

我所做的如下:

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

有了字母数组,我能够绕过5000项的阈值限制。每个字母最多会有1500个名称。

英文:

In my particular scenario, I had a column with an OrganizationName, with values starting with almost every alphabet.

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
  • 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. Each alphabet would have a maximum of 1500 names.

huangapple
  • 本文由 发表于 2020年1月6日 17:38:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609724.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定