Tìm kiếm trang web

SharePoint Online: Xóa hàng loạt tất cả các mục danh sách bằng PowerShell


Yêu cầu: Xóa hàng loạt mục danh sách SharePoint Online bằng PowerShell.

Xóa tất cả mục danh sách trong SharePoint Online bằng PowerShell:

Chúng tôi có một danh sách trong trang SharePoint Online - Office 365 với rất nhiều mục. Cần xóa chúng hàng loạt thay vì xóa từng cái một khỏi giao diện người dùng web SharePoint. Đây là tập lệnh để xóa hàng loạt mục danh sách trong SharePoint Online Office 365 bằng Mô hình đối tượng phía máy khách (CSOM) với PowerShell.


#Load SharePoint CSOM Assemblies
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"
  
#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
}

Xin lưu ý, các mục trong danh sách này không được chuyển vào Thùng rác mà bị xóa vĩnh viễn! Tập lệnh PowerShell này có thể khá hữu ích khi xử lý việc xóa các mục hàng loạt khỏi một danh sách lớn. Đây là một PowerShell khác để xóa tất cả các mục danh sách trong SharePoint Online: Xóa tất cả các mục khỏi danh sách SharePoint Online bằng PowerShell

Bài viết liên quan