Sometimes you need to prevent users to add your web part to pages. You can do this simple when deactivate a feature. Removing the web part is based on assembly name.
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
string assemblyName;
assemblyName = System.Reflection.Assembly.GetExecutingAssembly().FullName;
using (SPSite site = (SPSite)properties.Feature.Parent)
{
using (SPWeb web = (SPWeb)site.OpenWeb())
{
if (web.CurrentUser.IsSiteAdmin)
{
List<int> toDelete = new List<int>();
SPList list = site.GetCatalog(SPListTemplateType.WebPartCatalog);
foreach (SPListItem item in list.Items)
{
if (item["WebPartAssembly"].ToString() == assemblyName)
{
toDelete.Add(item.ID);
break;
}
}
foreach (int i in toDelete)
{
SPListItem item = list.GetItemById(i);
item.Delete();
}
list.Update();
}
}
}
}
No comments:
Post a Comment