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

[玩转系统] 使用 PowerShell 从 SharePoint 网站集中的所有页面中删除 Web 部件

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

使用 PowerShell 从 SharePoint 网站集中的所有页面中删除 Web 部件


要求:将 SharePoint 从 SharePoint 2013 迁移到 SharePoint 2016 后,我们决定从所有使用该 Web 部件的页面中删除该部件。

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

要从页面中删除 Web 部件,您可以编辑页面并简单地删除 Web 部件。如果页面崩溃,请使用 Web 部件维护模式,将“?contents=1”附加到 URL,然后删除不需要的 Web 部件。例如,https://intranet.crescent.com/SitePages/Home.aspx?contents=1

[玩转系统] 使用 PowerShell 从 SharePoint 网站集中的所有页面中删除 Web 部件

PowerShell 从网站集中的所有页面中删除 Web 部件:

我们可以利用 PowerShell 从网站集中的所有页面中删除特定的 Web 部件,而不是从浏览器的每个页面中删除 Web 部件。


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Configuration parameters
$SiteURL = "https://intranet.crescent.com"
$WebPartTypeName="*BambooCalendar*"

#Get All Subsites in a site collection and iterate through each
$Site = Get-SPSite $SiteURL
ForEach($Web in $Site.AllWebs)
{

    Write-host Processing $Web.URL
    # If the Current Web is Publishing Web
    if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($Web))
    {
        #Get the Publishing Web 
        $PubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($Web)
                   
        #Get the Pages Library
        $PagesLib = $PubWeb.PagesList  #Pages Library
     }
     else
     {
        $PagesLib = $Web.Lists["Site Pages"]
     } 

        #Iterate through all Pages  
        foreach ($Page in $PagesLib.Items | Where-Object {$_.Name -match ".aspx"}) 
        {
            $WebPartsToDelete = @()
            $PageURL=$web.Url+"/"+$Page.File.URL
            write-host $pageurl
            $WebPartManager = $Page.File.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
                 
            #Get All Web Parts of given type
            foreach ($WebPart in $WebPartManager.WebParts | Where-Object { $_.GetType().ToString() -like $WebPartTypeName} )
            {
                write-host "Found the Web Part at "$PageURL -f Green
                $WebPartsToDelete += $WebPart.ID
            }
            #If the web part to delete is found
            If($WebPartsToDelete)
            {
                #Checkout if required
                if ($Page.File.RequiresCheckout) 
                { 
                    if ($Page.File.CheckOutStatus -ne "None") 
                    {  
                        write-host "Overriding Checkout..."
                        $Page.File.UndoCheckOut()
                    }
                    write-host "Checking Out..."
                    $Page.File.CheckOut()
                }

                #Remove the web part from page          
                foreach ($WebPart in $WebPartsToDelete)
                {
                   Write-Host "Deleting Web Part on $($web.Url)/$($page.Url)"
                   $WebPartManager = $Page.File.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)       
                   $WebPartManager.DeleteWebPart($WebPartManager.webParts[$webPart])
                }
                
                if ($Page.File.CheckOutStatus -ne "None") 
                { 
                    Write-host "CheckIn-In..."
                    $Page.File.CheckIn("Deleted web part")
                    $Page.File.Publish('')
                }

                if ($Page.ParentList.EnableModeration) 
                { 
                    write-host "Approving..."
                    $Page.File.Approve('') 
                }
            }
        }
}

此脚本会迭代整个网站集中的所有网站和页面,并删除给定的 Web 部件。

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

取消回复欢迎 发表评论:

关灯