Deleting and Closing Web Parts
One of the major tasks over looked during page performance issues trouble shooting is how many closed web parts are there on the page. SharePoint offer end users and administrator’s two options for removing web parts from a page: close and delete.
Closing a web part does not remove the web part from the page but rather sets it to invisible. The web part’s configuration and customization are rendered as part of the page but simple not visible to end users. During content authoring an author may add a web part to a page and close it in an attempt to clear the page and start over again. This may be repeated several times until he/she gets the configuration right. The caveat of this is that even though the web part seems to have been removed, it has only been set to invisible and all the multiple instances will still be rendered as page of the page when that page is requested. The feature has its benefits; in the sense that you may not need to reconfigure or customize a web part every time you remove and wish to add it back. It saves a lot of time especially when dealing with complex web parts.
So the take home message is that, at any point in time make sure only one web part exists on a page with either open or closed as its status. Delete any duplicates or multiples as the delete operation remove the web part and its configuration permanently from the page. There is no recycle bin for capability for web part customization so you need to be absolutely sure before deleting a web part.
To see all web parts on a page and their status use: http://[url]/[page.aspx]?contents=1.
The web parts present of the page will be listed and their status (closed / open) stated. I will urge you to remove or multiple closed instances of a web part and see the immediate of your page load time decrease immediately.
If you have too many closed web pages on multiple pages to remove. Use the power shell script below. Given the root site collection or any site collection, this script will iterate through the sub sides and all their pages and permanently remove all closed web arts. Script has not yet been tested on 2010 but it should work.
—————————————————————————————————————————————————————
[void][reflection.assembly]::Loadwithpartialname(“Microsoft.SharePoint”) | out-null
[void][reflection.assembly]::Loadwithpartialname(“Microsoft.SharePoint.SPWeb”) | out-null
[void][reflection.assembly]::Loadwithpartialname(“Microsoft.Office.Server.Search”) | out-null
[void][reflection.assembly]::Loadwithpartialname(“Microsoft.Office.Server”) | out-null
# Returns the SPSite at the specified URL
function get-spsite ([String]$webUrl=$(throw ‘Parameter -webUrl is missing!’))
{
return New-Object -TypeName “Microsoft.SharePoint.SPSite” -ArgumentList “$webUrl”
}
# Returns the SPSite object from the specified URL
function get-spweb ([String]$webUrl=$(throw ‘Parameter -webUrl is missing!’))
{
$site = New-Object -TypeName “Microsoft.SharePoint.SPSite” -ArgumentList “$webUrl”
return $site.OpenWeb()
}
# Returns the SPList object from the specified URL and list name
function get-splist ([String]$webUrl=$(throw ‘Parameter -webUrl is missing!’),
[String]$listName=$(throw ‘Parameter -listName is missing!’))
{
$site = New-Object -TypeName “Microsoft.SharePoint.SPSite” -ArgumentList “$webUrl”
$web = $site.OpenWeb()
return $web.Lists[$listName]
}
function GetHiddenWebParts ([String]$sourceWebUrl=$(throw ‘Parameter -sourceWebUrl is missing!’))
{
$writer.writeline(” Page URL; Closed WebPart Name “)
$sourceWeb = Get-SPWEB $sourceWebUrl
foreach($sourceWeb in $sourceWeb.Webs)
{
RecursivelyCheckForWebSites $sourceWeb.url
}
$SourceWeb.Dispose
}
function RecursivelyCheckForWebSites( [String]$sourceWeburl)
{
GetHiddenWebPartInfo $sourceWeburl
if($sourceWebUrl)
{
$sourceWeb = Get-SPWEB $sourceWebUrl
if($sourceWeb.Webs.Count -gt 0)
{
foreach($tempsourceWeb in $sourceWeb.Webs)
{
RecursivelyCheckForWebSites $tempsourceWeb.url
}
}
$SourceWeb.Dispose
}
}
function GetHiddenWebPartInfo ([String]$sourceWeburl)
{
if($sourceWebUrl)
{
$currentWeb = Get-SPWEB $sourceWebUrl
if($currentWeb.Lists["Pages"])
{
$SourceWebPagelist = $currentWeb.Lists["Pages"]
foreach($currentPage in $SourceWebPagelist.items)
{
try
{
$webPartManager = $currentWeb.GetLimitedWebPartManager($currentPage.url,
[System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$currentPageWebParts = $webPartManager.WebParts
foreach($WebPart in $currentPageWebParts)
{
if($WebPart.IsClosed)
{
write-host $currentWeb.url’/'$currentPage.url $WebPart.title
$writer.writeline($currentWeb.url+”/”+$currentPage.url+” ; “+$WebPart.title)
//delete webpart
currentPageWebParts.Delete(WebPart.StorageKey);
}
}
}
catch
{
#Page does not exist
}
}
}
$currentWeb.Dispose
}
}
$File =”[path]\ DeletedWebPartResult.txt”
$writer = new-object System.IO.StreamWriter($File)
$mainsite = ‘[site collection]’
GetHiddenWebParts $mainsite
$writer.Close()




