Sunday, October 18, 2015

Delete webpart from SharePoint Page using Powershell script


$mySiteTempURL = "http://contoso.com/"            
$siteUrl = $mySiteTempURL
$spWeb = Get-SPWeb $siteUrl -ErrorAction Stop

#Declare the absolute path to the SharePoint page
$pagePath = "/default.aspx"
$pageUrl = $siteUrl + $pagePath
write-host "Processing site: ", $siteUrl
write-host "Processing page: ", $pageUrl
#Initialise the Web part manager for the specified profile page.
$spWebPartManager = $spWeb.GetLimitedWebPartManager($pageUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
#List all the Webparts in the specified page
#foreach ($webpart in $spWebPartManager.WebParts)
#{
#    write-host $siteUrl +": Existing Web part - " + $webpart.Title + " : " + $webpart.ID
#}
#Remove the Share Documents Web part from that page
foreach ($webpart in ($spWebPartManager.WebParts | Where-Object {$_.Title -eq "Colleagues"}))
{
   write-host $siteUrl +": Existing Web part - " + $webpart.Title + " : " + $webpart.ID
   $webpart1 = $webpart
   break;
}

#Delete the existing webpart
$spWebPartManager.DeleteWebPart($spWebPartManager.WebParts[$webpart1.ID])
write-host "Deleted the existing Shared Document web part."
$spWeb.Dispose()

No comments:

Post a Comment