I am developping a web part that I can’t update on the SharePoint server without deleting it in webpart gallery. Then, I run in power shell :
- Update-SPSolution …
- Disable-SPFeature …
- Enable-SPFeature …
I tried to delete the web part in the gallery programmatically in the event receiver, but it causes SharePoint to fail :
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;
using (SPWeb web = site.RootWeb)
{
SPList list = web.Lists["Web Part Gallery"];
// go through the items in reverse
for (int i = list.ItemCount - 1; i >= 0; i--)
{
// format name to look like a feature name
string webpartName = list.Items[i].Name;
webpartName = webpartName.Substring(0, webpartName.IndexOf('.'));
// delete web parts that have been added
if (properties.Feature.Definition.DisplayName == webpartName)
{
list.Items[i].Delete();
break;
}
}
}
}
Any idea what am i doing wrong ? thx
I am coming back to share the solution.
Here is the correct code to delele the web part from the gallery when deactivating the feature :
Finally, after adding this code in the event receiver, the procedure for updating the Web Part solution is :
Disable-SPFeature …
Uninstall-SPFeature …
Update-SPSolution …
Install-SPFeature …
Enable-SPFeature …
Hope it helps