So I haven’t been around blogging for a while due to having a little downtime and finishing up a project I’ve been working on. I’ve just started a new project which involves migrating a very large SPS 2003 implementation to MOSS 2007.
Hopefully I’ll be back blogging regularly with some useful SharePoint related articles and to start off with I’m going to demonstrate how to delete all WebParts from a WebPart page programmatically.
This requirement came up recently as I needed to create a feature to delete all WebParts from the standard MySite template and add some custom WebParts when every MySite was provisioned.
The steps to accomplish this were as follows:
- Create a feature that when activated deleted all WebParts from the page and added my custom ones on.
- Create a feature stapler to staple my new feature to the SPSPERS template so the feature is activated each time a new MySite was provisioned.
I’m only going to cover step 1 here as there are plenty of great articles out there that explain feature stapling e.g. http://blogs.msdn.com/cjohnson/archive/2006/11/01/feature-stapling-in-wss-v3.aspx
Ok first we need to create a class that inherits from the SPFeatureReceiver class like so:
public class CustomMySiteWebPartsFeature : SPFeatureReceiver
The SPFeatureReceiver class is an abstract class with the following four abstract methods:
- FeatureActivated(SPFeatureReceiverProperties properties)
- FeatureDeactivating(SPFeatureReceiverProperties properties)
- FeatureInstalled(SPFeatureReceiverProperties properties)
- FeatureUninstalling(SPFeatureReceiverProperties properties)
We need to override all four of these methods but we only need to write our code in the FeatureActivated method so leave the others blank.
The first thing we do is get an instance of the SPWeb class which we can do by using the properties variable that is passed to the method.
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
using (SPWeb web = properties.Feature.Parent as SPWeb)
{ }
}
Next we need to check that this is definately a MySite page we are working on just in case the feature gets activated on another site. We can do this by checking the WebTempate property of our SPWeb instance.
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
using (SPWeb web = properties.Feature.Parent as SPWeb)
{
if (web.WebTemplate == “SPSPERS” || web.WebTemplate == “SPSMSITEHOST”)
{
}
}
}
To actually manipulate WebParts on our page we need to get an instance of the SPLimitedWebPartManager
class like this:
SPLimitedWebPartManager manager = web.GetLimitedWebPartManager(“default.aspx”,
PersonalizationScope.Shared);
And as you can see we call the GetLimitedWebPartManager method of our SPWeb instance for the page we want to manipulate.
Now we can begin to delete the WebParts. Below is the code I used to accomplish this.
List<Microsoft.SharePoint.WebPartPages.WebPart> webParts = new
List<Microsoft.SharePoint.WebPartPages.WebPart>();
foreach (Microsoft.SharePoint.WebPartPages.WebPart webPart in manager.WebParts)
{
webParts.Add(webPart);
}
foreach (Microsoft.SharePoint.WebPartPages.WebPart webPart in webParts)
{
manager.DeleteWebPart(webPart);
}
I first add all the WebParts to a collection then interate over that collection calling the DeleteWebPart method of our SPLimitedWebPartManager instance. We need to do this because calling DeleteWebPage while interating over the WebParts collection causes an exception as the collection has been altered during the enumeration.
When this is done we can use the AddWebPart method of the SPLimitedWebPartManager passing it an instance of the WebPart you want to add, the WebPartZone id you want to add the WebPart to and the index at which you want to add the WebPart within the zone.
After all this is done don’t forget to dispose the SPLimitedWebPartManager instance and call Update on the SPWeb instance:
manager.Dispose();
web.Update();
And that’s all there is to it.
Hope it was helpful.