Tìm kiếm trang web

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


Yêu cầu: Xóa Mục danh sách trong SharePoint Online bằng PowerShell.

Trong bài đăng blog này, chúng tôi sẽ hướng dẫn bạn cách sử dụng PowerShell để xóa một mục danh sách khỏi danh sách SharePoint Online của bạn. Chúng tôi sẽ chia sẻ một số ví dụ về PowerShell để xóa mục danh sách theo ID, xóa mục danh sách theo Tiêu đề và xóa mục danh sách khớp với các điều kiện nhất định. Chúng tôi sẽ hướng dẫn các bước cần thiết để kết nối với PowerShell, sau đó xóa một mục khỏi danh sách. Bắt đầu nào!

Làm cách nào để xóa mục danh sách trong SharePoint Online?

Để xóa một mục danh sách khỏi danh sách SharePoint Online, hãy làm như sau:

  1. Duyệt đến danh sách của bạn và chọn mục bạn muốn xóa.
  2. Nhấp vào nút “Xóa” trên thanh menu của danh sách.

  3. Xác nhận lời nhắc. Mục được chọn sẽ được gửi vào thùng rác.

Bạn cũng có thể xóa mục danh sách bằng cách nhấp chuột phải vào mục đó và chọn “Xóa” từ menu ngữ cảnh.

SharePoint Online PowerShell để xóa mục danh sách

PowerShell là một công cụ đa năng có thể được sử dụng để quản lý và tự động hóa các tác vụ trong SharePoint Online. Hãy xem cách sử dụng PowerShell để xóa một mục danh sách. Điều này có thể hữu ích nếu bạn cần nhanh chóng xóa một số dữ liệu khỏi danh sách hoặc nội dung nào đó trong danh sách mà bạn không còn cần nữa. Đây là tập lệnh PowerShell của tôi để xóa một mục khỏi danh sách SharePoint Online:


#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"

Function Delete-ListItem()
{
  param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $ItemID
    )

    Try {
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
        
        #Get the List and item
        $List = $Ctx.Web.Lists.GetByTitle($ListName)
        $ListItem = $List.GetItemById($ItemID)
        $ListItem.DeleteObject()
        $Ctx.ExecuteQuery()

        Write-Host "List Item Deleted successfully!" -ForegroundColor Green

    }
    Catch {
        write-host -f Red "Error Deleting List Item!" $_.Exception.Message
    } 
}

#Parameters
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$ItemID="1"

#Call the function to rename column
Delete-ListItem -SiteURL $SiteURL -ListName $ListName -ItemID $ItemID

SharePoint Online: Xóa hàng loạt mục trong danh sách khớp với điều kiện đã cho

Làm cách nào để xóa hàng loạt mục danh sách SharePoint Online bằng PowerShell? Giả sử chúng tôi muốn xóa tất cả các dự án không hoạt động khỏi danh sách “Dự án”.


#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"

Function Delete-ListItems()
{
  param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $CAMLQuery
    )

    Try {
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
        
        #Define Query to get List Items
        $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
        $Query.ViewXml =$CAMLQuery
        
        #Get the List
        $List = $Ctx.Web.Lists.GetByTitle($ListName)

        #Get All List Items matching the query
        $ListItems = $List.GetItems($Query)
        $Ctx.Load($ListItems)
        $Ctx.ExecuteQuery()

        Write-host "Total Number of Items Found to delete: $($ListItems.count)"
        if($ListItems.count -gt 0)
        {
            #Loop through each item and delete
            For ($i = $ListItems.Count-1; $i -ge 0; $i--)
            {
                $ListItems[$i].DeleteObject()
            } 
            $Ctx.ExecuteQuery()

            Write-Host "List Items Deleted successfully!" -ForegroundColor Green
        }
    }
    Catch {
        write-host -f Red "Error Deleting List Items!" $_.Exception.Message
    } 
}

#Parameters
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$CAMLQuery="<View><Query><Where><Eq><FieldRef Name='IsActive' /><Value Type='Boolean'>0</Value></Eq></Where></Query></View>"

#Call the function to rename column
Delete-ListItems -SiteURL $SiteURL -ListName $ListName -CAMLQuery $CAMLQuery 

PnP PowerShell để xóa mục danh sách trong SharePoint Online

Để xóa một mục danh sách cụ thể theo ID của nó, hãy sử dụng lệnh ghép ngắn PnP PowerShell Remove-PnPListItem:


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName ="Projects"
$ListItemID=7

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Remove List Item by ID
Remove-PnPListItem -List $ListName -Identity $ListItemID -Recycle -Force

Xóa mục danh sách theo tiêu đề trong SharePoint Online bằng PnP PowerShell

Tương tự, Bạn có thể xóa các mục danh sách dựa trên các giá trị trường lọc. Bạn cũng có thể sử dụng lệnh ghép ngắn Move-PnPListItemToRecycleBin để di chuyển một mục danh sách vào thùng rác!


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName ="Projects"
$ListItemTitle="SharePoint Online Migration Project"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the List Item by "Title" field value
$Query = "<View><Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>$ListItemTitle</Value></Eq></Where></Query></View>"
$ListItem = Get-PnPListItem -List $ListName -Query $Query -PageSize 1

#Removes the List item
If($ListItem -ne $null)
{
    Remove-PnPListItem -List $ListName -Identity $ListItem -Force
}

Làm cách nào để xóa tất cả các mục khỏi danh sách SharePoint trong PowerShell? Nếu bạn muốn xóa tất cả các mục khỏi danh sách SharePoint Online bằng PowerShell, hãy sử dụng: SharePoint Online: Xóa tất cả các mục danh sách bằng PowerShell

Bài viết liên quan