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

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

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

SharePoint Online:使用 PowerShell 删除列表


要求:删除 SharePoint Online 中的列表。

如何删除 SharePoint 中的列表?

当不再需要时,您可能希望删除 SharePoint Online 网站上可用的任何列表或库。或者您可能想要删除列表以释放一些存储空间或删除不相关的列表和库以保持协作空间干净。无论如何,无论是什么原因,要删除 SharePoint Online 中的列表,请执行以下操作:

  1. 单击站点设置齿轮 >> 从菜单中选择站点内容。
  2. 在网站内容页面上,将鼠标悬停在要删除的列表上,然后单击下拉菜单图标 (...)。
  3. 在出现的菜单上,单击“删除”链接并确认提示将列表发送到回收站。
  4. 或者,您可以通过转至“设置>>列表设置”页面>>单击“权限和管理”组下的“删除此列表”来删除 SharePoint Online 中的列表。

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

与经典体验类似,在现代 SharePoint Online 网站上,您可以从相应列表上下文菜单中删除网站内容页面中的列表:

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

具有 SharePoint 管理员、所有者或编辑者权限的任何人都可以删除该列表。请记住,删除列表后,它不会被永久删除,并且可以在 93 天内恢复。好吧,现在让我们使用 PowerShell 删除 SharePoint Online 中的列表。

使用 PowerShell 删除 SharePoint Online 中的列表

如果您想在不使用浏览器界面的情况下删除 SharePoint Online 中的列表,该怎么办?因为有时您可能需要在 SharePoint 环境中删除特定的隐藏列表或自动删除列表。让我们了解如何使用 PowerShell 删除 SharePoint Online 中的列表。以下是要删除的 SharePoint Online PowerShell 列表。

在运行此脚本之前,请确保您的计算机上已安装 SharePoint Online Management Shell 或 SharePoint Online CSOM SDK。使用 Windows PowerShell 或 PowerShell ISE 编辑和运行脚本。


#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"
  
#Set Variables for Site collection URL and List Name
$SiteURL= "https://crescent.sharepoint.com/sites/pmo"
$ListName="Projects"

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Cred

    #Get the List by name
    $List=$Ctx.Web.Lists.GetByTitle($ListName)

    #powershell delete sharepoint online list
    $List.DeleteObject()
    $Ctx.ExecuteQuery()
        
    Write-host -f Green "List Deleted Successfully!" 
}
Catch {
    write-host -f Red "Error deleting list!" $_.Exception.Message
}

这将使用 PowerShell 永久删除 SharePoint Online 列表。您还可以使用此 PowerShell 删除 SharePoint Online 文档库!

使用 PowerShell 删除 SharePoint Online 列表

如果您没有找到列表的“删除”选项怎么办?好吧,让我们打开允许删除标志,然后通过使用 PowerShell-CSOM 将列表发送到 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"

#Custom function to delete a SharePoint Online list using powershell
Function Delete-SPOList
{
    param
    (
        [string]$SiteURL  = $(throw "Please Enter the Site URL!"),
        [string]$ListName = $(throw "Please Enter the List Name to Delete!")
    )
    Try {
        #Get Credentials to connect
        $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 set "AllowDeletion" flag to True
        $List=$Ctx.Web.Lists.GetByTitle($ListName)
        
        #sharepoint online powershell allow deletion
        $List.AllowDeletion = $True
        $List.Update()
        $Ctx.ExecuteQuery()
             
        #Delete the List - Send to Recycle bin
        $List.Recycle() | Out-Null  
        $Ctx.ExecuteQuery()
 
        Write-Host "List: '$ListName' has been Deleted Successfully!" -ForegroundColor Green  
    }
    Catch {
        write-host -f Red "Error Deleting List!" $_.Exception.Message
    }
}

#Variables
$SiteURL="https://Crescent.sharepoint.com/sites/projects"
$ListName = "Projecs v1"

#Call the function
Delete-SPOList -SiteURL $SiteURL -ListName $ListName

这将使用 PowerShell 删除 SharePoint Online 中的列表。

在 SharePoint Online 中删除大型列表怎么样?
您可能会收到一条错误消息:“尝试的操作被禁止,因为它超出了管理员强制执行的列表视图阈值。”当您尝试删除 SharePoint Online 中的大型列表或库时。要解决此问题,您必须先删除所有列表 SharePoint 列表项,然后删除 SharePoint 列表!

使用 PnP PowerShell 删除 SharePoint Online 列表

还可以使用 PnP PowerShell 删除列表或库。确保您的计算机上安装了 PnP PowerShell 模块,并连接到您的 SharePoint Online 网站。连接后,使用Remove-PnPList cmdlet 删除所需的列表或库。此 cmdlet 还将删除列表或库中的所有项目。

以下是删除 SharePoint Online 列表的 PnP PowerShell:


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$ListName ="Project Docs"

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

#Check if List exists
If(Get-PnPList -Identity $ListName)
{
    #sharepoint online powershell remove list
    Remove-PnPList -Identity $ListName -Force
    Write-host -f Green "List '$ListName' Deleted Successfully!"
}
Else
{
    Write-host -f Yellow "Could not find List '$ListName'"
}

Remove-PnPList 会永久删除该列表。使用“-Recycle”开关将列表或库发送到回收站! “Identity”参数可以是 ID 或列表的标题。

当您想要删除 SharePoint Online 中的文档库时,可以使用类似的脚本:使用 PowerShell 删除 SharePoint Online 文档库

经常问的问题:

如何使用 PowerShell 脚本从 SharePoint Online 删除文件夹?

若要使用 PowerShell 脚本从 SharePoint Online 删除文件夹,可以使用 Remove-PnPFolder 命令。例如,“Remove-PnPFolder -Name $FolderName -Folder $ParentFolderSiteRelativeURL -Force -Recycle”
以下是完整的脚本:如何使用 PowerShell 删除 SharePoint Online 中的文件夹?

如何删除处于保留或保留策略状态的 SharePoint 列表?

在保留或保留政策期间,无法删除包含项目的 SharePoint 列表或库!您必须首先删除所有项目,包括列表或库中的文件和文件夹,才能删除保留策略或电子取证保留下的列表。
更多信息:列表在保留或保留期间无法删除政策

如何永久删除 SharePoint 中的列表?

如果您要从 Web 浏览器界面删除列表,请确保将其从第一阶段和第二阶段回收站中删除。对于 PowerShell,请在脚本中调用“Delete()”方法而不是“Recyle()”。

如何在 SharePoint Online PowerShell 中删除文件夹和子文件夹?

如果要递归删除文件夹及其子文件夹和文件,请使用此 PnP PowerShell 脚本:PowerShell 在 SharePoint Online 中删除包含文件和子文件夹的文件夹

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

取消回复欢迎 发表评论:

关灯