当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:使用 PowerShell 批量删除所有列表项

作者:精品下载站 日期:2024-12-14 14:58:58 浏览:15 分类:玩电脑

SharePoint Online:使用 PowerShell 批量删除所有列表项


要求:使用 PowerShell 批量删除 SharePoint Online 列表项目。

[玩转系统] SharePoint Online:使用 PowerShell 批量删除所有列表项

使用 PowerShell 删除 SharePoint Online 中的所有列表项:

我们在 SharePoint Online - Office 365 网站中有一个包含大量项目的列表。需要批量删除它们,而不是从 SharePoint Web UI 中一一删除。以下是使用客户端对象模型 (CSOM) 和 PowerShell 在 SharePoint Online Office 365 中批量删除列表项的脚本。


#Load SharePoint CSOM Assemblies
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"
  
#Variables for Processing
$SiteUrl = "https://Crescent.sharepoint.com/sites/Marketing"
$ListName="LargeList"
$BatchSize = 100

#Setup Credentials to connect
$Cred = Get-Credential
  
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
  
    #Get the List
    $List=$Ctx.web.Lists.GetByTitle($ListName)
    $Ctx.Load($List)
    $Ctx.ExecuteQuery()
    Write-host "Total Number of Items Found in the List:"$List.ItemCount

    Write-Host -f Yellow -NoNewline "Deleting List Items from List '$ListName'"
  
    #Set a Flag
    $Continue = $True
    While($Continue)
    {
        Write-Host -NoNewline -f Yellow "."

        #Get List Items in Batches
        $Query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery($BatchSize, "ID")
        $ListItems = $List.GetItems($Query)
        $Ctx.Load($ListItems)
        $Ctx.ExecuteQuery()
                
        If($ListItems.Count -gt 0)
        {
            #Delete List Items
            for($i = $ListItems.Count-1; $i -ge 0; $i--)
            {
                $ListItems[$i].DeleteObject()
            }
            $Ctx.ExecuteQuery()
        }
        Else
        {
            $Continue = $False
        }
    }
    Write-Host -f Green "`nAll List Items Deleted Successfully!"
    
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

请注意,这些列表项不会移至回收站,而是永久删除!在处理从庞大列表中删除批量项目时,此 PowerShell 脚本可能非常有用。以下是另一个用于删除 SharePoint Online 中所有列表项的 PowerShell:使用 PowerShell 从 SharePoint Online 列表中删除所有项目

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯