Remove Event Receiver from SharePoint List on Feature Deactivation

Information

Consider a case where you have added an event receiver to SharePoint List on Feature Activation. Now for best SharePoint practices we need to remove the event receiver from SharePoint List on feature deactivation.

I have seen at many places where people have used some weird methods to steps to delete the event receiver from SharePoint list like using PowerShell. To avoid these glitches please find below is the sample code to remove event receiver from SharePoint List.

Assumption

You are writing this code on Site Level Feature Deactivation.

Solution

Place this code in feature deactivation method in Feature Event Receiver. 

  1. /// <summary>    
  2. /// the method below will handle the event raised after a feature has been deactivated.     
  3. /// </summary>    
  4. /// <param name="properties">object of SPFeatureReceiverProperties</param>    
  5. public override void FeatureDeactivating(SPFeatureReceiverProperties properties)    
  6. {    
  7.     try    
  8.     {    
  9.         if (properties != null)    
  10.         {    
  11.             SPSite site = properties.Feature.Parent as SPSite;    
  12.             var siteID = ((SPSite)properties.Feature.Parent).ID;    
  13.   
  14.             using (var spsite = new SPSite(siteID))    
  15.             {    
  16.                 using (var spWeb = spsite.OpenWeb(site.RootWeb.ID))    
  17.                 {    
  18.                     SPList oSharePointList = spWeb.Lists.TryGetList("SharePointListName");    
  19.   
  20.                     if (oSharePointList != null)    
  21.                     {    
  22.                         SPEventReceiverDefinitionCollection oSPEventReceiverDefinitionCollection = oSharePointList.EventReceivers;    
  23.   
  24.                         List<SPEventReceiverDefinition> oRecieversToDelete = new List<SPEventReceiverDefinition>();    
  25.   
  26.                         foreach (SPEventReceiverDefinition oReciever in oSPEventReceiverDefinitionCollection)    
  27.                         {    
  28.                             if (oReciever != null && oReciever.Assembly.Equals(System.Reflection.Assembly.GetExecutingAssembly().FullName))    
  29.                             {    
  30.                                 oRecieversToDelete.Add(oReciever);    
  31.                             }    
  32.                         }    
  33.                         foreach (SPEventReceiverDefinition oSPEventReceiverDefinition in oRecieversToDelete)    
  34.                         {    
  35.                             oSPEventReceiverDefinition.Delete();    
  36.                         }    
  37.                         oSharePointList.Update();    
  38.                     }    
  39.                 }    
  40.             }    
  41.         }    
  42.     }    
  43.     catch (Exception ex)    
  44.     {    
  45.         //catch exception here    
  46.         throw;    
  47.     }    
  48. }  

Note: You need to replace the List name with the name of the list you are operating on.

Happy SharePointing !!!
Ebook Download
View all
Learn
View all