Monday, 12 November 2012

Deleting the web part from page using PowerShell scripts

Hello Again,

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

So here I will again step by step discuss how we achieved it using PowerShell script.

Here we are considering name of the deleting web part is “MyCustomWP”, available in ZoneId 1 and order 

1.     Create the web instance and get publishing web as follows
$webURL = “”
$web = Get-SPWeb $webURL
[Microsoft.SharePoint.Publishing.PublishingWeb]$pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web);
2.     Get the default value for AllowUnsafeUpdates property to change it at the end and now set it to true
$allowunsafeupdates = $web.AllowUnsafeUpdates
$web.AllowUnsafeUpdates = $true

3.     Get the Pages library and get the page from where we want to delete the web part
$list = $web.Lists[$pagesLibrary]
$page = $web.GetFile(”My Page URL from where I want to delete the web part”)


4.     If page is not checked out to other user, then checkout the page, get the webpartmanager for the page and get all webparts available on the page
$webpartmanager = $web.GetLimitedWebPartManager($page.URL, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$WebPartCollection = $webpartmanager.WebParts

5.     Go through all the web part collection, compare with our webpart name, ZoneId and Order.
$webPartsCount = $WebPartCollection.Count

for($i=0;$i -lt $WebPartCollection.Count; $i++){
#Here we are checking the RepresentedWebPartType.Name – for details on this please refer

if($webpartmanager.Webparts[$i].RepresentedWebPartType.Name -eq $webPartName -or $webpartmanager.Webparts[$i].GetType().Name -eq        $MyCustomWP){
                                                                                    if($webpartmanager.Webparts[$i].ZoneId -eq 1){
                                                                                                if($webpartmanager.Webparts[$i].PartOrder -eq 1){
            $SPWebPart = $web.GetWebPartCollection($page.url,[Microsoft.SharePoint.WebPartPages.Storage]::Shared)
            $webPartTobeDeleted = $webpartmanager.Webparts[$i]                                                                    $SPWebPart.Delete($webPartTobeDeleted.StorageKey)
            Write-Output "Deleted" $webPartTobeDeleted.GetType().Name
            }#if part order
            }#if webpart zone
            }#if webpart type
            }#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
    if ($page.CheckedOutBy.UserLogin -eq $web.CurrentUser.UserLogin)
        $page.CheckIn("Page checked in automatically by PowerShell script")
        Write-Output $page.Title"("$page.Name") has been checked in"
#Approve the page
$page.Approve("Page approved automatically by PowerShell script")

$web.AllowUnsafeUpdates = $allowunsafeupdates


Your suggestions / Feedback / Comments always welcome !

Enjoy SharePoint J

No comments: