用PowerShell脚本删除SharePoint 的 Page中的WebPart

编写PowerShell脚本可以删除page中所有的webpart,也可以根据webpart的属性信息去删除特定的webpart。

下面的PowerShell脚本便是删除对应page中所有的webpart:

param(
    [string] $webUrl = "http://........:11111/sites/mysite",
    [string] $pageUrl = "/Pages/myPage1.aspx"
)
$web = Get-SPWeb $webUrl
[Microsoft.SharePoint.Publishing.PublishingWeb]$pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web);
$allowunsafeupdates = $web.AllowUnsafeUpdates
$web.AllowUnsafeUpdates = $true
$list = $web.Lists["Pages"]
if($list){
    $page = $web.GetFile($webUrl + $pageUrl)
}
$page.CheckOut()
$webpartmanager = $web.GetLimitedWebPartManager($page.URL, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
for($i=0;$i -lt $webpartmanager.WebParts.Count; $i++){
    $webpartmanager.DeleteWebPart($webpartmanager.WebParts[$i]);
    Write-Output "Deleted" $webpartmanager.WebParts[$i].title
}
if ($page.CheckedOutBy.UserLogin -eq $web.CurrentUser.UserLogin)
{
    Write-Output $web.CurrentUser.UserLogin    
    $page.CheckIn("Page checked in automatically by PowerShell script")
    Write-Output $page.Title"("$page.Name") has been checked in"
}

$web.Update()
$web.AllowUnsafeUpdates = $allowunsafeupdates
$web.Dispose()

 

。。。。。。。。。

你可能感兴趣的:(SharePoint)