Skip to content

Instantly share code, notes, and snippets.

@mirontoli
Created June 24, 2014 12:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mirontoli/af0dabda7a4b6af494d2 to your computer and use it in GitHub Desktop.
Save mirontoli/af0dabda7a4b6af494d2 to your computer and use it in GitHub Desktop.
#existing stuff, do not copy:
$url = "https://some.contoso.com"
asnp *share*
#remove all webparts
$step = "Deleting Existing Webparts"
$pageUrl = $url + "/Pages/default.aspx"
$site = new-object microsoft.sharepoint.spsite $pageUrl
$web = $site.OpenWeb()
$pageItem = $web.GetListItem($pageUrl)
$requresCheckout = $pageItem.ParentList.ForceCheckout
$requiresApproval = $pageItem.ParentList.EnableModeration
$wpManager = $web.GetLimitedWebPartManager($pageUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$webparts = $wpManager.WebParts
if ($webparts.Count -gt 0) {
if ($requresCheckout) { $pageItem.File.CheckOut("Online",$null) }
$wpManager.WebParts | % { $wpManager.DeleteWebPart($_.ID) }
if ($requresCheckout) { $pageItem.File.CheckIn("Removed all web parts",[Microsoft.SharePoint.SPCheckinType]::MajorCheckIn) }
if ($requiresApproval) { $pageItem.File.Approve("Automatic Approve. (Administrator)") }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment