0

I have a question regarding a feature and the webparts. Basically recently i noticed that when i deploy a webpart with my solution and it is bound to a feature on site collection scope, when it is deployed the feature is automatically activated so when i'm in page i can see amongst all the webparts to choose, i see my own web part and i can add it to the page.

So I add the web part to the page and it works ...

Now i go and deactivate the feature, what i see is that the web part is still present on the page AND i can still ADD the web part to any page in the site collection, how come?

Maybe i do something wrong or is it normal behaviour and have can i make sure that when i deactivate the web part it is not anymore available in the web parts catalog?

Any input is welcome, i need a clarification on this one ...

1
  • I'm not 100% sure, but do you deactivate it only in the site collection and not in the centraladministratin?
    – Sascha
    Jan 23, 2013 at 8:27

2 Answers 2

2

As mentioned by @Alexander, the webpart still remains in the WebPart gallery. You will need to write the code in FeatureDeactivating event which will look for your webpart in the webpart gallery and remove it from there. Follwoing is the sample code which needs to be written in the event:

int itemToDelete = 0;
SPList list = parentSite.GetCatalog(SPListTemplateType.WebPartCatalog);
foreach (SPListItem item in list.Items)
{
    if (item["Web Part"].ToString().ToLower() == "name.webpart")
    {
        itemToDelete = item.ID;
        break;
    }
}
if (itemToDelete > 0)
{               
    SPListItem item = list.GetItemById(itemToDelete);
    item.Delete();
}
list.Update();
5
  • hmm link doesn't work, is it normal? )
    – Shkipper
    Jan 23, 2013 at 10:41
  • I'm just curious: Why do you first collect the ID of the webpart and delete it afterwards? Is it technically possible that there is more than one entry in the webpart gallery matching "name.webpart"?
    – jcp
    Feb 4, 2014 at 13:46
  • Answer to your first question is that if you try to delete item in the first foreach loop itself, you will get error that you cant modify a collection. As far as second question is concerned, it is not possible to have to have two webparts with same name. Updated the answer for that. Feb 4, 2014 at 14:28
  • 1
    You could use a CAML query instead of looping though the list. Something like this "<Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='File'>name.webpart</Value></Eq></Where>". Then you would not need any loop.
    – gaotter
    Nov 6, 2014 at 14:45
  • @gaotter, you are right. Nov 6, 2014 at 14:56
1

As I understand your solution is deployed, thus you have assembly with web part codebehind in the GAC. When you activate feature, the web part definition file is deployed to the web parts library. If you diactivate your feature this file still presents into this library. I think that SharePoint doesn't remove it because it thinks that this file can be important for your. So you have deployed definition and assembly in the GAC, thus it works correct. If you would like to remove this web part go to site settings -> web part gallery, find and delete yourname.webpart file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.