Script Delete Closed Web Parts
Does anyone have a script to delete closed web parts from SharePoint 2007?
May 18th, 2010 11:04pm
Hi,
Would you please let us know what kind of script you wish to use?
For JavaScript, you can use OOB webpart JS function
MSOLayout_RemoveWebPart for this.
Code like this:
-----------------------------------------------------------------------------------------
javascript:MSOLayout_RemoveWebPart(document.all['YourWebpartID'])"
-----------------------------------------------------------------------------------------
Please refer the following article:
http://aaclage.blogspot.com/2008/09/link-button-to-delete-current.html
You could implement the code in a Content Edit Webpart.
Hope this can help.
Best Regards,
Aaron
TechNet Subscriber Support
in forum
If you have any feedback on our support, please contact
tngfb@microsoft.com
Free Windows Admin Tool Kit Click here and download it now
May 19th, 2010 8:18am
Hi,
You can delete closed web parts using Web Parts Maintenance page, add "?contents=1" to the URL of page (on which closed web parts are present).
On Web Parts Maintenance page, select the closed web parts and select delete.
Hope this helps.
Regards.
May 19th, 2010 8:05pm
Hi,
Would you please let us know how is your problem going? Are all suggestions helpful for your issue?
If you need further assistance, please feel free to let us know.
Have a nice day!
Best Regards
Aaron
TechNet Subscriber Support
in forum
If you have any feedback on our support, please contact
tngfb@microsoft.com
Free Windows Admin Tool Kit Click here and download it now
May 21st, 2010 12:14pm
Hi,
Would you please let us know how is your problem going?
If you need further assistance, please feel free to let us know.
Have a nice day!
Best Regards
Aaron
TechNet Subscriber Support in forum
If you have any feedback on our support, please contact
tngfb@microsoft.com
May 25th, 2010 6:55am
I know about the "?contents=1. I have over 2000 sites and I can't go to all of them using the "?contents=1. I would like to know if there is a powershell script or something that can help me clean them all up.
Free Windows Admin Tool Kit Click here and download it now
May 25th, 2010 10:10pm
Hi,
Thanks for your feedback!
If you wish to use PowerShell, then you can use SharePoint Object Module in your script.
Please try the following:
------------------------------------------------------------------------------
[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") | out-null
[void][System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
# set-up working variables...
#define your site URL
$targetSiteUrl=”http://test/sites/teamtest”
#define your page URL
$pageURL="http://test/sites/teamtest/default.aspx"
# set-up working variables...
write-host "delete Webparts"
$spSite = new-object Microsoft.SharePoint.SPSite($targetSiteUrl)
$web=$spSite.Openweb()
$webpartmanager=$web.GetLimitedWebPartManager($pageURL, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
foreach ($webpart in $webpartmanager.WebParts)
{
if ($webpart.Title = "CellProvider")
{
# You can set some conditions heres
$webpartmanager.CloseWebPart($webpart);
$webpartmanager.SaveChanges($webpart);
}
}
write-host "delete finished."
------------------------------------------------------------------------------
Some more reference:
Please refer the following articles for deleting a webpart instance with SPLimitedWebPartManager, you just need to translate this to PowerShell script.
http://leedale.wordpress.com/2008/04/15/programmatically-removing-all-webparts-from-a-page-in-moss-2007/
http://www.sharepointdevwiki.com/display/public/How+to+delete+Web+Part+instances+on+pages+using+Feautures+-+FeatureReceiver+-+SPLimitedWebPartManager
This article includes some PowerShell code for manipulating SharePoint webparts, you can also take a look:
http://www.sharepointdevwiki.com/display/public/How+to+create+Web+Part+instances+on+pages+using+PowerShell+and+SPLimitedWebPartManager
Hope this can help.
Best Regards,
Aaron
TechNet Subscriber Support
in forum
If you have any feedback on our support, please contact
tngfb@microsoft.com
May 26th, 2010 5:23am
Here is the script that I use to delete all closed web parts on all sites and all webs. This works for SharePoint 2010 (I've never tried it on 2007). It only looks at the
default.aspx pages in the root of the web and in the \pages folder of the web. Hope it works for you too!
[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") | out-null
[void][System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$pageURL="default.aspx"
function SubWebs([Microsoft.SharePoint.SPWeb] $web)
{
$subwebs = $web.GetSubwebsForCurrentUser()
foreach($subweb in $subwebs)
{
SubWebs($subweb)
$subweb.Dispose()
}
$web | select Url
$file = $web.GetFile($pageURL)
$webpartmanager=$file.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$count = $webpartmanager.WebParts.Count
if ($count -eq 0) {
$file = $web.GetFile("pages\" + $pageURL)
$webpartmanager=$file.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$count = $webpartmanager.WebParts.Count
}
for ($i=$count;$i -ge 0;$i--)
{
$webpart = $webpartmanager.WebParts[$i]
#If the webpart is closed, remove it
if ($webpart.IsClosed -eq $TRUE)
{
if ($file.RequiresCheckout -eq $TRUE) { $webpartmanager.Web.GetFile($file.UniqueID).CheckOut()}
$webpartmanager.DeleteWebPart($webpart)
if ($file.RequiresCheckout -eq $TRUE) { $webpartmanager.Web.GetFile($file.UniqueID).CheckIn("Deleted web part: " + $webpart.Title)}
write-host $webpart.Title "deleted from" $web.URL
}
}
}
function Pause ($Message="Press any key to continue...")
{
Write-Host -NoNewLine $Message
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Write-Host ""
}
write-host "This script will delete all web parts on this server that are"
write-host "currently in a closed (not displayed) status."
pause
$oContentService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService;
[Microsoft.SharePoint.Administration.SPWebApplicationCollection]$waColl = $oContentService.WebApplications;
$waColl1 = $waColl | where-object {$_.IsAdministrationWebApplication -eq $FALSE}
foreach ($wa in $waColl1)
{
$waName = $wa.Name
$sites = $wa.Sites
foreach ($obj in $sites)
{
$spSite = new-object Microsoft.SharePoint.SPSite($obj.Url)
$web=$spSite.Openweb()
If ($web -ne $null)
{
SubWebs $web
$web.Dispose()
}
}
}
Free Windows Admin Tool Kit Click here and download it now
June 30th, 2010 5:58pm