O365 and SharePoint: Remove the WebPart from a site by using PowerShell

As we aware the WebpartManager class is helpful in Server side object model. The question popped-up in my mind “Is this possible by using PowerShell scripting?“.

I found answer as “Yes”.

Below is the code snippet to remove webpart from a site by specifying webpartId and site url,

$siteURL = “ht tp://sharePoint/sites/specialsite”; # first constant: site URL
$webpartId = “<guid of your choice for webpart>; # second argument: webpart GUID
$pagepath = “/default.aspx” # change page name or page path here
$pageURL = $siteURL + $pagepath
write-host “Processing site: “, $siteURL
Write-host “Processing page: ” , $pageURL
write-host “Processing webpart ID: ” , $webpartID
$site=new-object Microsoft.SharePoint.SPSite($siteURL)
$web=$site.Openweb()
$webpartmanager=$web.GetLimitedWebPartManager($pageURL, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$webpartmanager.DeleteWebPart($webpartmanager.Webparts[$webpartId])
$web.Update()
$web.Dispose()
write-host “Finished.”

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s