When de-activitating a Site Feature (ie Scope = Web);
class WebInstaller: SPFeatureReceiver
{
const string WebPartTitle = "The WebPart Title";
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
//do nothing
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
try
{
int ItemID = -1;
SPWeb oWeb = (SPWeb) properties.Feature.Parent;
using(SPWeb oRootWeb = oWeb.Site.RootWeb)
{
SPList oList = oRootWeb.Lists["Web Part Gallery"];
for (int i = 0; i < oList.ItemCount; i++)
{
if (oList.Items[i].Title.Equals(WebPartTitle))
{
ItemID = oList.Items[i].ID;
break;
}
}
if (ItemID != -1)
{
SPListItem oItem = oList.GetItemById(ItemID);
oItem.Delete();
oList.Update();
}
}
}
catch (Exception e)
{
//will throw to and error page
throw new Exception(String.Format("The has been an error "
+ "removing the WebPart from the Gallery; <br /><br />{0}", e.ToString()));
}
}
public override void FeatureInstalled(SPFeatureReceiverProperties properties)
{
//do nothing
}
public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
{
//do nothing
}
}
When de-activitating a Site Collection Feature (ie Scope = Site);
class SiteInstaller: SPFeatureReceiver
{
const string WebPartTitle = "The WebPart Title";
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
//do nothing
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
try
{
int ItemID = -1;
using(SPSite oSite = (SPSite)(properties.Feature.Parent))
{
using(SPWeb oWeb = oSite.RootWeb)
{
SPList oList = oWeb.Lists["Web Part Gallery"];
for (int i = 0; i < oList.ItemCount; i++)
{
if (oList.Items[i].Title.Equals(WebPartTitle))
{
ItemID = oList.Items[i].ID;
break;
}
}
if (ItemID != -1)
{
SPListItem oItem = oList.GetItemById(ItemID);
oItem.Delete();
oList.Update();
}
}
}
}
catch (Exception e)
{
//will throw to and error page
throw new Exception(String.Format("The has been an error "
+ "removing the WebPart from the Gallery; <br /><br />{0}", e.ToString()));
}
}
public override void FeatureInstalled(SPFeatureReceiverProperties properties)
{
//do nothing
}
public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
{
//do nothing
}
}
