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

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

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

SharePoint Online:使用 PowerShell 删除列表视图


要求:删除 SharePoint Online 中的视图。

如何删除 SharePoint Online 中的视图?

您是否正在寻找一种方法来删除 SharePoint Online 中不再需要的列表视图?这篇博文将向您展示如何删除列表视图。

要删除 SharePoint Online 中的列表视图,请执行以下步骤:

  1. 登录到您的 SharePoint Online 网站,然后导航到包含要删除的视图的列表。
  2. 选择要删除的视图。单击“修改此视图”(或转到列表设置,单击相应的视图标题)
  3. 在编辑视图页面上,单击“删除”按钮。确认一次提示以从 SharePoint Online 列表或库中删除视图。

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

请注意,除了默认视图之外,您可以删除 SharePoint Online 列表和库的任何现有视图。如果要删除默认视图,必须先将不同的视图设置为默认视图,然后删除前一个视图!

SharePoint Online:使用 PowerShell 删除自定义视图

如果您遇到视图问题并且无法从 Web 用户界面将其删除,您可以使用 PowerShell 删除 SharePoint Online 中的列表视图。以下是用于删除 SharePoint Online 中的视图的 PowerShell:


#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"
$ListName= "Projects"
$ViewName="Active Projects"

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
    $List = $Ctx.Web.Lists.GetByTitle($ListName)

    #Get the view to delete
    $View = $List.Views.getByTitle($ViewName)
    
    #Delete the list view
    $View.DeleteObject()
    $Ctx.ExecuteQuery()

    Write-host "View '$ViewName' deleted Successfully!" -ForegroundColor Green
 }
Catch {
    write-host -f Red "Error Deleting List View!" $_.Exception.Message
}

删除视图后,您可以通过刷新列表视图页面并检查该视图是否不再列出来验证该视图是否已被删除。您还可以返回列表设置并检查视图列表以确认视图已被删除。

用于删除 SharePoint Online 中的列表视图的 PowerShell 脚本:

上面的脚本可以包装成可重用的函数。具体方法如下:


#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 Remove-ListView()
{
    param(
        [Parameter(Mandatory=$true)][string]$SiteURL,
        [Parameter(Mandatory=$false)][System.Management.Automation.PSCredential] $Cred,
        [Parameter(Mandatory=$true)][string]$ListName,
        [Parameter(Mandatory=$true)][string]$ViewName
    )

    Try {
        $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
        $List = $Ctx.Web.Lists.GetByTitle($ListName)

        #Get the view to delete
        $View = $List.Views.getByTitle($ViewName)
    
        #Delete the list view
        $View.DeleteObject()
        $Ctx.ExecuteQuery()

        Write-host "View '$ViewName' deleted Successfully!" -ForegroundColor Green
     }
    Catch {
        write-host -f Red "Error Deleting List View!" $_.Exception.Message
    }
}

#Call the function to delete list view
Remove-ListView -SiteURL "https://crescent.sharepoint.com" -Cred (Get-Credential) -ListName "Projects" -ViewName "Active Projects"

SharePoint Online:使用 PnP PowerShell 删除视图

在 SharePoint Online 中,您可以使用 PnP PowerShell 中的 Remove-PnPView cmdlet 删除不再需要的列表视图:


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

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred

    #Remove List View
    Remove-PnPView -List $ListName -Identity $ViewName -Force -ErrorAction Stop
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

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

取消回复欢迎 发表评论:

关灯