I have a webpart i build using WSpBuilder and it works OK. When i use Deploy from Visual Studio it installs the WSP sharepoint and add the webpart to the webpart gallery. But it seems the feature does activate the webpart but i also want to remove it when deactivating it.(So i know i need to add something to the FeatureDeactivating

So i created a Feature With Reciever template and added it to the project.Which created a folder FeatureWithReceiver with the element and feature xml files and also a FeatureCode folder with FeatureWithReceiver.cs

 public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        SPSite site = null;


        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–)
          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 (webpartName == "myStoreComms")






        if (site != null)


In the feature.xml i have this below

<?xml version="1.0" encoding="utf-8"?>
<Feature Id="5d910db3-5adf-4f8e-a102-05d5a1653c59"
     Description="Description for FeatureWithReceiver"
     ReceiverAssembly="MWO.SharePoint, Version=, Culture=neutral, PublicKeyToken=c9faae97dc801bd2"
  <ElementManifest Location="elements.xml"/>


<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <Module Name="WebPartPopulation" Url="_catalogs/wp" RootWebOnly="TRUE">
        <File Url="myStoreComms.webpart" Type="Ghostable">
            <Property Name="Group" Value="MyGroup"></Property>
            <Property Name="QuickAddGroups" Value="MyGroup" />

But when i go to the site features i can see the feature but when i activate it it does puts the webpart in the gallery but when i deactivate it it doesn't remove it but i get error:

Failed to instantiate file "myStoreComms.webpart" from module "WebPartPopulation": Source path "myStoreComms.webpart" not found.

Any ideas.. .what could be wrong and thanks in advance

1 Answer 1


You are disposing the SPSite referenced by properties.Feature.Parent

Since you did not instantiate this SPSite, you should not dispose it. Change this first.

If that didn't fix it then attach the debugger. Does it throw an exception within your block of code? If so, on what line? It could be that the exception is being thrown after your code executes because you changed some state in your code (e.g. you disposed an SPSite which SharePoint needs to complete Feature deactivation) which affects further execution.

Your Answer

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

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