none
FeatureDeactivating does not remove the EventReceivers

    Question

  •  

    Hi,

     

    I did some calls to the eventhandler at FeatureActivated method

     

    public override void FeatureActivated(SPFeatureReceiverProperties properties)

    {

    SPList lstCost = site.Lists["Cost"];

    string sAssemblyInfo = ~Some Assembly Info~;

    string sListReceiverName = ~Some List Reciever Name~;

    lstCost.EventReceivers.Add(SPEventReceiverType.ItemDeleting, sAssemblyInfo, sListReceiverName);

    lstCost.EventReceivers.Add(SPEventReceiverType.ItemAdding, sAssemblyInfo, sListReceiverName);

    lstCost.EventReceivers.Add(SPEventReceiverType.ItemUpdating, sAssemblyInfo, sListReceiverName);

    }

     

     

    at the FeatureDeactivating method i have not assigned any codes

     

    and later i found that after installing the feature all the eventhandlers are installed but the upon uninstalling the feature the eventhandlers still exist to the sharepoint list. I used the SharePoint Tips Utility Pack to examine the event handler in that specific list.

     

    how can i remove the event handlers that i added completely upon uninstalling the features. Is there some codes need to be written at FeatureDeactivating method?

     

    Tools Used

    Sharepoint 2007

    VS2005

    .net 2.0/3.5

     

    thank you

    Thursday, February 21, 2008 7:14 AM

Answers

  • Alternatively you could use a different approach.

     

    You can build Features that add Event Recievers to specific list types. Although this is more generic (as it will add to every list of the same type) it will automatically add the event handler when activated, and remove the event handler when deactivated.

     

    There is a good article on the Wrox Website here:

     

    And some examples from that article are below:

     

    Example: Feature.xml

     

    Code Snippet

     

    <?xml version="1.0" encoding="utf-8"?>
    <Feature Id="<b>85DA483B-C3D4-4b5c-8F3A-89331E996305</b>" 
        Title="<b>EventHandlerExample1</b>"
        Description="<b>An event handler which will cancel the deletion of an announcements list item and insert a new item announcing that a delete attempt was made</b>"
        Scope="<b>Web</b>"
        xmlns="http://schemas.microsoft.com/sharepoint/">
    	<ElementManifests>
    		<ElementManifest Location="Elements.xml"/>
    	</ElementManifests>
    </Feature>

     

     

     

    Example: Associated Elements.xml

     

    Code Snippet

     

    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    	<Receivers ListTemplateId="104">
    		<Receiver>
    			<Name>EventHandlerExample1</Name>
    			<Type>ItemDeleting</Type>
    			<SequenceNumber>20000</SequenceNumber>
    			<Assembly>
    				EventHandlerExample1, Version=1.0.0.0,
    				culture=neutral, PublicKeyToken=db6373fbcacd33ee
    			</Assembly>
    			<Class>EventHandlerExample1.CancelAnnouncementDeleteHandler</Class>
    			<Data></Data>
    			<Filter></Filter>
    		</Receiver>
    	</Receivers>
    </Elements>

     

     

    Thursday, February 21, 2008 12:38 PM

All replies

  • How can you expect SP to magically delete the event handler automatically when you registered it manually through code Smile

    Yes, you will have to delete through OM, just like Add, do a remove.

    Madhur
    Thursday, February 21, 2008 11:48 AM
  • Alternatively you could use a different approach.

     

    You can build Features that add Event Recievers to specific list types. Although this is more generic (as it will add to every list of the same type) it will automatically add the event handler when activated, and remove the event handler when deactivated.

     

    There is a good article on the Wrox Website here:

     

    And some examples from that article are below:

     

    Example: Feature.xml

     

    Code Snippet

     

    <?xml version="1.0" encoding="utf-8"?>
    <Feature Id="<b>85DA483B-C3D4-4b5c-8F3A-89331E996305</b>" 
        Title="<b>EventHandlerExample1</b>"
        Description="<b>An event handler which will cancel the deletion of an announcements list item and insert a new item announcing that a delete attempt was made</b>"
        Scope="<b>Web</b>"
        xmlns="http://schemas.microsoft.com/sharepoint/">
    	<ElementManifests>
    		<ElementManifest Location="Elements.xml"/>
    	</ElementManifests>
    </Feature>

     

     

     

    Example: Associated Elements.xml

     

    Code Snippet

     

    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    	<Receivers ListTemplateId="104">
    		<Receiver>
    			<Name>EventHandlerExample1</Name>
    			<Type>ItemDeleting</Type>
    			<SequenceNumber>20000</SequenceNumber>
    			<Assembly>
    				EventHandlerExample1, Version=1.0.0.0,
    				culture=neutral, PublicKeyToken=db6373fbcacd33ee
    			</Assembly>
    			<Class>EventHandlerExample1.CancelAnnouncementDeleteHandler</Class>
    			<Data></Data>
    			<Filter></Filter>
    		</Receiver>
    	</Receivers>
    </Elements>

     

     

    Thursday, February 21, 2008 12:38 PM
  • Here's a bit of code I use to remove events when I deactive a feature that is tied to a specific list instance (as opposed to the template):

     

    Code Snippet

    public override void FeatureDeactivating(SPFeatureReceiverProperties properties)

    {

      SPWeb site = (SPWeb)properties.Feature.Parent;

      SPList docLib = site.Lists["test"];

      String asmName = [the assembly name, same as used to add the handler.]

     

      int i;

      for (i = 0; i < docLib.EventReceivers.Count; i++)

      {

        if (docLib.EventReceivers[i].Assembly.Equals(asmName))

          docLib.EventReceivers[i].Delete();

      } // looping thru event receivers.

     

      docLib.Update();

      site.Update();

    }

     

     

     

    --Paul Galvin, Conchango
      RSS @ http://feeds.feedburner.com/PaulGalvinsSharepointSpace
      Web site @ http://paulgalvin.spaces.live.com

     

    • Proposed as answer by Tom Resing Saturday, June 6, 2009 8:30 PM
    Friday, February 22, 2008 2:12 AM
  • thank you and this works well :-)

    Monday, February 25, 2008 11:10 AM
  • So are you implying here that

    1. The handler will be added to any list of id 104 that already exists before the feature is activated?

    and

    2. The handler will be added automatically to any subsequent list of id 104 that is created after the feature is activated?
    Tuesday, September 22, 2009 6:49 PM