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

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

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

SharePoint Online:使用 PowerShell 删除列表项


要求:使用 PowerShell 删除 SharePoint Online 中的列表项。

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

在这篇博文中,我们将向您展示如何使用 PowerShell 从 SharePoint Online 列表中删除列表项。我们将分享一些 PowerShell 示例,用于按 ID 删除列表项、按标题删除列表项以及删除符合给定条件的列表项。我们将逐步完成连接到 PowerShell 所需的步骤,然后从列表中删除项目。让我们开始吧!

如何删除 SharePoint Online 中的列表项?

要从 SharePoint Online 列表中删除列表项,请执行以下操作:

  1. 浏览到您的列表,然后选择您要删除的项目。
  2. 单击列表菜单栏中的“删除”按钮。

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

  3. 确认提示。所选项目将被发送到回收站。

您还可以通过右键单击列表项并从上下文菜单中选择“删除”来删除列表项。

SharePoint Online PowerShell 删除列表项

PowerShell 是一种多功能工具,可用于管理和自动执行 SharePoint Online 中的任务。让我们看看如何使用 PowerShell 删除列表项。如果您需要快速从列表中删除某些数据或列表中不再需要的内容,这会很有用。以下是我的 PowerShell 脚本,用于从 SharePoint Online 列表中删除项目:


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

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:批量删除符合给定条件的列表项目

如何使用 PowerShell 批量删除 SharePoint Online 列表项?假设我们要从“项目”列表中删除所有不活动的项目。


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

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 

用于删除 SharePoint Online 中的列表项的 PnP PowerShell

要按 ID 删除特定列表项,请使用此 PnP PowerShell cmdlet 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

使用 PnP PowerShell 在 SharePoint Online 中按标题删除列表项

同样,您可以根据过滤字段值删除列表项。您还可以使用 Move-PnPListItemToRecycleBin cmdlet 将列表项移动到回收站!


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

如何在 PowerShell 中删除 SharePoint 列表中的所有项目?如果要使用 PowerShell 删除 SharePoint Online 列表中的所有项目,请使用:SharePoint Online:使用 PowerShell 删除所有列表项目

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

取消回复欢迎 发表评论:

关灯