Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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 ...

share|improve this question
    
I'm not 100% sure, but do you deactivate it only in the site collection and not in the centraladministratin? –  Sascha Jan 23 '13 at 8:27
add comment

2 Answers 2

up vote 1 down vote accepted

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();
share|improve this answer
    
hmm link doesn't work, is it normal? ) –  Shkipper Jan 23 '13 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 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. –  Nadeem Yousuf Feb 4 at 14:28
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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