Recently, I have had the need to remove all the web parts of the welcome page of a large number of sites. The number of sites was so big that the option of doing it manually was out. It is in these cases where we can always rely on Windows PowerShell, because the time invested in developing a script is profitable, plus we are learning along the way.

So, to delete a Web Part using Windows PowerShell, this would be the core of the script:

#Getting WebPartManager.
$spWpManager = $oWeb.GetLimitedWebPartManager($welcomePageUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared);
		
# Array to save the ID of Web Parts.
$webPartsIDsList = @();
		
#For every WebPart, we save the ID in the previus array.
$spWpManager.Webparts | ForEach-Object { $webPartsIDsList = $webPartsIDsList + $_.ID; }
		
#Finally we delete the WebPart, and publish de page..
foreach ($id in $webPartsIDsList)
{
	for	($i = 0; $i -lt $count + 2; $i++) {Write-Host "---" -Nonewline;}
	Write-Host "Deleting" $spWpManager.Webparts[$id].Title -Nonewline;;
	$spWpManager.DeleteWebPart($spWpManager.Webparts[$id]);
	Write-Host "...OK!" -ForegroundColor green;
	
}
		
PublishPage($page);
$oWeb.Update();

 

As we see, to access the web parts of page in question, we need an instance of the WebPartManager class. With it we have access to all Web Parts, so we can insert, delete, update, etc… In the example, we are deleting all web parts from the Welcome Page, obtaining first the ID of each of them and saving it in a array. We could be more precise specifying a Web Part. Here come into play the needs of each one.

I hope this can be helpful šŸ™‚

 

SolidQ

SolidQ suministra servicios para plataformas Microsoft que le ayudan a diseƱar, integrar y optimizar su utilizaciĆ³n de datos