SharePoint: Deleting the Web Part From Page Using PowerShell Scripts

One of the requirements we have in our project is to delete the web part from a page. So one of the obvious options is to delete the web part using a PowerShell script.

So here I will again step-by-step discuss how to do this using a PowerShell script.

Here we are using “MyCustomWP”as the name of the web part to delete, available in ZoneId 1 and order 1.

  1. Create the web instance and get the publishing web as follows:
    1. $webURL = “http://MyWeb.com”  
    2. $web = Get-SPWeb $webURL  
    3. [Microsoft.SharePoint.Publishing.PublishingWeb]$pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web); 
  2. Get the default value for the AllowUnsafeUpdates property to change it at the end and now set it to true:
    1. $allowunsafeupdates = $web.AllowUnsafeUpdates  
    2. $web.AllowUnsafeUpdates = $true 
  3. Get the Pages library and get the page from where we want to delete the web part:
    1. $list = $web.Lists[$pagesLibrary]  
    2. if($list){  
    3. $page = $web.GetFile(”My Page URL from where I want to delete the web part”)  
    4.   

  4. If the page is not checked out to another user, then checkout the page, get the webpartmanager for the page and get all webparts available on the page.
    1. $page.CheckOut()  
    2. $webpartmanager = $web.GetLimitedWebPartManager($page.URL, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)  
    3. $WebPartCollection = $webpartmanager.WebParts 
  5. Go through all the web part collections and compare them with our webpart name, ZoneId and Order.
    1. $webPartsCount = $WebPartCollection.Count  
    2.   
    3. for($i=0;$i -lt $WebPartCollection.Count; $i++){ 
    Here we are checking the RepresentedWebPartType.Name. For details click here.
    1. if($webpartmanager.Webparts[$i].RepresentedWebPartType.Name -eq $webPartName -or $webpartmanager.Webparts[$i].GetType().Name -eq        $MyCustomWP){  
    2.                             if($webpartmanager.Webparts[$i].ZoneId -eq 1){  
    3.                                 if($webpartmanager.Webparts[$i].PartOrder -eq 1){  
    4.     $SPWebPart = $web.GetWebPartCollection($page.url,[Microsoft.SharePoint.WebPartPages.Storage]::Shared)  
    5.     $webPartTobeDeleted = $webpartmanager.Webparts[$i]                      $SPWebPart.Delete($webPartTobeDeleted.StorageKey)  
    6.     Write-Output "Deleted" $webPartTobeDeleted.GetType().Name  
    7.     $web.Update()  
    8.     break  
    9.     }#if part order  
    10.     }#if webpart zone  
    11.     }#if webpart type  
    12.     }#foor loop - $i 
  6. Finally check in the page and close all the instances.

    Check to ensure the page is checked out by you and if so, check it in
    1. if ($page.CheckedOutBy.UserLogin -eq $web.CurrentUser.UserLogin)  
    2. {  
    3.     $page.CheckIn("Page checked in automatically by PowerShell script")  
    4.     Write-Output $page.Title"("$page.Name") has been checked in"  

    Approve the page.
    1. $page.Approve("Page approved automatically by PowerShell script")  
    2.   
    3. $web.AllowUnsafeUpdates = $allowunsafeupdates  
    4. $pubWeb.Close()  
    5. $web.Dispose() 
Thanks!

Your suggestions, feedback and comments are always welcome!