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

[玩转系统] SharePoint Online:使用 PowerShell 从页面中删除 Web 部件

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

SharePoint Online:使用 PowerShell 从页面中删除 Web 部件


要求:使用 PowerShell 从 SharePoint Online 页面中删除 Web 部件。

如何从 SharePoint Online 页面删除 Web 部件?

Web 部件是用于显示信息、添加业务功能、实现流程、充当数据库接口或提供任何其他特定于应用程序的功能的小部件。如果不再需要 SharePoint Online 页面上的 Web 部件,可以将其删除。您可以通过以下方式删除 Web 部件:

  1. 导航到放置 Web 部件的页面 >> 从设置菜单中单击“编辑页面”。
  2. 从 Web 部件下拉菜单中选择删除选项并确认提示。
  3. 这将从页面中删除 Web 部件。

PowerShell 从 SharePoint Online 页面中删除 Web 部件:

我们在 SharePoint Online 页面上有一个脚本编辑器 Web 部件,标题为“Google Analytics 脚本”,并要求使用 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 to remove a web part from SharePoint Online page
Function Remove-SPOWebpartFromPage([String]$SiteURL,[String]$PageRelativeURL,[String]$WebPartTitle, [PSCredential]$Cred)
{ 
     Try
     {
        #Setup Credentials to connect
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
        #Set up the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
  
        #Get the web parts from the given web page
        $Web = $Ctx.web
        $Ctx.Load($Web)
        $Page= $Web.GetFileByServerRelativeUrl($PageRelativeURL)
        $WPM = $Page.GetLimitedWebPartManager("Shared")
        $Ctx.Load($WPM)
        $WebParts = $WPM.WebParts
        $Ctx.Load($WebParts)
        $Ctx.ExecuteQuery()
        write-host "Loaded web parts..."

        Write-host "Searching Web parts..."
        #Get the Web part ID to remove
        $WebPartID=[string]::Empty
        ForEach($WebPart in $WebParts)
        {
            #Get Web part properties
            $Ctx.Load($Webpart.WebPart.Properties)
            $Ctx.ExecuteQuery()            
            If($Webpart.WebPart.Properties.FieldValues["Title"] -eq $WebPartTitle)
            {
                $WebPartID = $Webpart.ID
            }
        }

        If($WebPartID -ne [string]::Empty)
        {
            $WebpartToRemove=$WPM.WebParts.GetById($WebPartID)
            $WebpartToRemove.DeleteWebPart()
            $Ctx.ExecuteQuery()
            write-host -f Green "Web Part '$($WebpartTitle)' has been removed!"
        }
        else
        {
            write-host -f Yellow "Web Part '$($WebpartTitle)' not found!"
        }        
   }
    Catch [System.Exception]
    {
        Write-Host -f Red $_.Exception.Message
    } 
} 

#Config parameters
$SiteURL = "https://crescent.sharepoint.com/"
$PageRelativeURL="/Sites/Marketing/SitePages/Home.aspx"
$WebPartTitle="Google Analytics Script"

#Get credentials to connect to 
$Credentials = Get-Credential

#Call the function to remove the web part from page
Remove-SPOWebpartFromPage -SiteURL $SiteURL -PageRelativeURL $PageRelativeURL -WebPartTitle $WebpartTitle -Cred $Credentials

使用 Web 部件 ID 删除 Web 部件:
以上脚本从 Web 部件标题的页面中删除 Web 部件。但是,如果您知道 Web 部件 ID,则可以通过以下方式简单地删除 Web 部件:


$WebPartID="a4877b9b-24b6-4a8d-8da1-95c36454fd5c"
$Webpart=$wpm.WebParts.GetById($webPartID)
$Webpart.DeleteWebPart()

要从新式 SharePoint Online 页面中删除 Web 部件,请使用:如何使用 PowerShell 从新式 SharePoint Online 页面中删除 Web 部件?

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

取消回复欢迎 发表评论:

关灯