SharePoint event receiver getting triggered multiple times

Problem – Event receiver is getting fired twice.

My event receiver was supposed to send email to creator upon item added event. The event receiver was triggered, though it was sending two emails every time an item was created.

This was an indication that there were two event receivers attached to my list.

Solution – I followed the  below steps to troubleshoot

Using PowerShell, first identify how many event receivers are attached to the respective list/library.

$site = Get-SPWEB -Identity "http://server:port/managedPath/siteColl/site/"
$web = $site.RootWeb
$list = $site.Lists["ListName"]
$list.EventReceivers | Select assembly, name, type

Below is the output
Blog1

So, as is evident, the list is associated with two event receivers and one instance needs to be deleted.
So I use the below code after the above powershell statements to delete any additional instance of event receiver associated with my list

$list.EventReceivers | Group-object assembly, class, type |  where { $_.Count -gt 1 } |  foreach { $_.Group[1..50] } | foreach { $_.Delete() }

Also, the same problem could be mitigated by writing the code to delete event receivers in feature de-activation.
Add event receiver to your feature and in the feature deactivation method, write the below code.

Note – The event receiver feature is web scoped.

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
    try
    {
        if (properties != null)
        {
             SPWeb web = properties.Feature.Parent as SPWeb;

             SPList oSharePointList = web.Lists.TryGetList("LISTNAME");

             if (oSharePointList != null)
             {
                 SPEventReceiverDefinitionCollection oSPEventReceiverDefinitionCollection = oSharePointList.EventReceivers;

                 List<SPEventReceiverDefinition> oRecieversToDelete = new List<SPEventReceiverDefinition>();

                 foreach (SPEventReceiverDefinition oReciever in oSPEventReceiverDefinitionCollection)
                 {
                      if (oReciever != null && oReciever.Assembly.Equals(System.Reflection.Assembly.GetExecutingAssembly().FullName))
                      {
                           oRecieversToDelete.Add(oReciever);
                      }
                  }
                  foreach (SPEventReceiverDefinition oSPEventReceiverDefinition in oRecieversToDelete)
                  {
                       oSPEventReceiverDefinition.Delete();
                   }
                   oSharePointList.Update();
                 }
              }
         }
         catch (Exception ex)
         {
             //catch exception here
             throw;
          }
     }

This code will remove the event receiver upon feature deactivation and would associate again upon activation.
 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s