How to Attach an Event Receiver to a SharePoint List on Feature Activation

Sathish Nadarajan
 
Solution Architect
December 14, 2015
 
Rate this article
 
Views
12414

For a very long time, I have been thinking to write this particular function. Here, there are two methods we can use. Either the Elements.xml or with the CS. I always believe in C Sharp, rather than the Elements File. Hence, I have come up with a Generic Method which will delete the event receiver and attach the event receiver to the List.

The Code snippet is self-explanatory and does not require much explanation.

 private static void AttachEventReceiver(SPWeb web,string listName,string eventClassName,string eventName,int sequenceNumber,string eventReceiverType)
         {
             SPList list = web.Lists[listName];
             if (list != null)
             {
                 SPEventReceiverDefinition eventReceiverDefinitionToDelete = null;
 
                 foreach (SPEventReceiverDefinition eventReceiverDefinition in list.EventReceivers)
                 {
                     if (eventReceiverDefinition.Class == eventClassName)
                     {
                         eventReceiverDefinitionToDelete = eventReceiverDefinition;
                         break;
                     }
                 }
 
                 if (eventReceiverDefinitionToDelete != null)
                 {
                     eventReceiverDefinitionToDelete.Delete();
                     list.Update();
                 }
 
                 SPEventReceiverDefinition workflowStatusUpdatedEventDefinition = list.EventReceivers.Add();
                 workflowStatusUpdatedEventDefinition.Assembly = System.Reflection.Assembly.GetExecutingAssembly().FullName;
                 workflowStatusUpdatedEventDefinition.Class = eventClassName;
                 workflowStatusUpdatedEventDefinition.SequenceNumber = sequenceNumber;
                 workflowStatusUpdatedEventDefinition.Name = eventName;
                 switch (eventReceiverType)
                 {
                     case "ItemUpdated":
                         workflowStatusUpdatedEventDefinition.Type = SPEventReceiverType.ItemUpdated ;
                         break;
                     case "CheckIn":
                         workflowStatusUpdatedEventDefinition.Type = SPEventReceiverType.ItemCheckedIn;
                         break;
                     case "ItemDeleting":
                         workflowStatusUpdatedEventDefinition.Type = SPEventReceiverType.ItemDeleting;
                         break;
                     default:
                         break;
                 }
                 
                 workflowStatusUpdatedEventDefinition.HostId = list.ID;
                 workflowStatusUpdatedEventDefinition.HostType = SPEventHostType.List;
                 workflowStatusUpdatedEventDefinition.Update();
                 list.Update();
 
             }
         }
 
 private static void DeleteEventReceiver(SPWeb web,string listName, string eventClassName)
         {
             SPList pagesList = web.Lists[listName];
 
             if (pagesList != null)
             {
                 SPEventReceiverDefinition eventReceiverDefinitionToDelete = null;
 
                 foreach (SPEventReceiverDefinition er in pagesList.EventReceivers)
                 {
                     if (er.Class == eventClassName)
                     {
                         eventReceiverDefinitionToDelete = er;
                         break;
                     }
                 }
 
                 if (eventReceiverDefinitionToDelete != null)
                 {
                     eventReceiverDefinitionToDelete.Delete();
                     pagesList.Update();
                 }
             }
         }
 

And the Sample Call to these methods are as follows.

 public override void FeatureActivated(SPFeatureReceiverProperties properties)
         {
             
             SPSite site = (SPSite)properties.Feature.Parent;
             SPWeb web = site.RootWeb;
             SPWeb confidentialWeb = site.AllWebs[SiteVariables.SITE_CONFIDENTIAL];
             AttachEventReceiver(web, "Pages", "EventReceiver Assembly Name", "Event Name", 1000, "CheckIn");
             
         }
 public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
         {
 
             SPSite site = (SPSite)properties.Feature.Parent;
             SPWeb web = site.RootWeb;
             SPWeb confidentialWeb = site.AllWebs[SiteVariables.SITE_CONFIDENTIAL];
             DeleteEventReceiver(web,"Pages"," EventReceiver Assembly Name ");
  
         }
 

Hope this snippet is useful.

Happy Coding,

Sathish Nadarajan.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment