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

      try
      {

        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")
            {

              list.Items[i].Delete();
              break;
            }

          }

        }

      }     

      finally
      {

        if (site != null)
          site.Dispose();

      }
}

In the feature.xml i have this below

<?xml version="1.0" encoding="utf-8"?>
<Feature Id="5d910db3-5adf-4f8e-a102-05d5a1653c59"
     Title="FeatureWithReceiver"
     Description="Description for FeatureWithReceiver"
     Version="12.0.0.0"
     Hidden="FALSE"
     Scope="Web"
     DefaultResourceFile="core"
     ReceiverAssembly="MWO.SharePoint, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c9faae97dc801bd2"
     ReceiverClass="MWO.SharePoint.FeatureWithReceiver"
     xmlns="http://schemas.microsoft.com/sharepoint/">
 <ElementManifests>
  <ElementManifest Location="elements.xml"/>
 </ElementManifests>
</Feature>

Element.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" />
        </File>
    </Module> 
  </Elements>

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

share|improve this question
add comment

1 Answer

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.

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.