Posts Tagged 'webpart remove'

How to remove webparts from a SharePoint page programmatically using Powershell

Let’s imagine this scenario:

You need to remove a webpart from a SharePoint page in your farm.   Easy, right?  Go to the page, enter the Edit mode, click on the webpart, select Delete.  No problem!   Now what if you had 1000 sites to remove the webpart from?   Doing it by hand is impractical, so we’ll need to use Powershell.

This will be a two step process:

Step 1, you need to run an inventory script which will generate a list of webparts and their GUIDs.

Step 2, you will use the webpart GUIDs from step 1 to run a removal script to remove the webparts.

STEP 1. INVENTORY SCRIPT

Execute the script below using the SharePoint 2010 Management Shell (PowerShell console).  This script does the following:

1. Iterate through all web applications in the farm and open every every site collection in each web app

2. For each site collection, access the homepage  “default.aspx” and capture a list of webparts on the page and their GUIDs.

3. Write the results to a file called “results.txt”, located in the same directory as the Powershell script.

The page name “default.aspx” is hard-coded in the script but can be easily changed to another page name or page path.

 

Script source code


[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") | out-null

$oContentService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService;

[Microsoft.SharePoint.Administration.SPWebApplicationCollection]$waColl = $oContentService.WebApplications;

$log = ".\results.txt"    # output file name and path
$pagepath = "/default.aspx"    # change page name or page path here

"Site URL; WebPart Title ; Webpart ID" | out-file $log

$waColl1 = $waColl | where-object {$_.IsAdministrationWebApplication -eq $FALSE}

foreach ($wa in $waColl1)
{

foreach ($obj in $wa.Sites) 
{

write-host "Processing: " , $siteURL

$siteURL = $obj.URL

$site=new-object Microsoft.SharePoint.SPSite($siteURL)

$pageURL = $siteURL + $pagepath  
$web=$site.Openweb()   
$webpartmanager=$web.GetLimitedWebPartManager($pageURL,  [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)   
foreach ($webpart in $webpartmanager.WebParts)
{   
$siteURL + "; " + $webpart.Title + " ;  " + $webpart.ID | out-file $log -append   
}                           
}

}

write-host "Finished."

 

STEP 2. WEBPART REMOVAL SCRIPT

This script  will programmatically remove a webpart from a SharePoint page.  It takes as arguments the target site URL and the webpart GUID.   The script targets the main page “default.aspx” but can be changed to accept any other page name or page path.

Notes: Use the results from the INVENTORY SCRIPT to create a list of webparts and their GUIDs to remove. Then build a script which calls out the WEBPART REMOVAL SCRIPT using the site URLs and webpart GUIDs as arguments.

The syntax is the following:

Execute the following command in SharePoint 2010 Management shell (Powershell console):

[WEBPART REMOVAL script file name] [site URL] [webpart GUID]

Example:

remove.ps1     http://mysharepointsite     a1b83-8tyu-6897-oiy8

 

Script source code


[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") | out-null [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.WebPartPages") [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Publishing")

$siteURL = $args[0]  # first argument: site URL

$webpartId = $args[1]   # 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."


RSS Information Week Headlines

  • An error has occurred; the feed is probably down. Try again later.

RSS SharePoint Team Blog

  • An error has occurred; the feed is probably down. Try again later.

RSS InfoPath Team Blog

  • An error has occurred; the feed is probably down. Try again later.

RSS Joel Oleson Blog

  • An error has occurred; the feed is probably down. Try again later.

RSS Susan Hanley’s KM Blog

  • An error has occurred; the feed is probably down. Try again later.

Blog Stats

  • 348,184 hits