none
Sharepoint Item events are fired twice

    Question

  • Hi,

    I have developed a custom feature to handle ItemAdded event of my list. In the event handler
    I am checking if the List name is "MyList" and then sent an email. I had no issues when using
    this feature in my development server. I created MyList in Production server and installed this
    feature. Now the event is getting fired twice and I am getting two mails for each item. I checked
    ItemAdding, ItemUpdating and ItemUpdated events and found that they are also fired twice. The
    feature is installed only once in the site. I looked into the stack trace but could not find
    anything useful. Any one knows what could be the reason? I am using WSS 3.0

    Thanks,
    Toms
    Monday, July 28, 2008 4:49 PM

Answers

  • Hi,
    I ran into the same issue of getting event fired twice. When I looked into the event recievers registered for the list (SPList.EventRecievers) I found that Item added event was registered twice.
    Deactivate the event reciever by deactivating the feature (If you have used feature to register the recievers)

    And delete the duplicate event reciever.

    There are many event recievers, so make sure you delete the correct one.

    SPEventReceiverDefinitionCollection eventcoll = list.EventReceivers;
    SPEventReceiverDefinition eventtodelete = null;
                         foreach (SPEventReceiverDefinition eventdef in eventcoll)
                        {
                            if (eventdef.Name=="LibItemAdded")
                            {
                                 eventtodelete=eventdef;
                            }
                        }
                        if(eventtodelete!=null)
                        {
                             eventdef.Delete();
                        }


    Now reactivate the feature to register the event recievers.
    You can recheck if only one event is registered.


    Situations like this can happen if you make a list template from site where this events was activated.
    Friday, January 16, 2009 7:57 PM

All replies

  • check this article, i think this is the case with your scenario, if you are using check in and save, it will fire twice, if you use publish only or check in only, it will be fired once only. 

    http://www.sharepoint-tips.com/2007/11/why-event-handlers-are-triggered-twice.html
    Monday, July 28, 2008 4:58 PM
  • I checked the link. My scenario is different. I am creating a new List Item and my ItemAdded event is getting fired twice. There is no check in involved.
    Monday, July 28, 2008 6:21 PM
  • I ran into this same issue, and it turned out that the event was firing before the PostBack was fired, and again after the postback.  You might want to put some code that checks to see if PostBack is set to true. 

    Monday, July 28, 2008 7:29 PM
  • I do not think that would be the case because I have the same code working perfectly in my development server. Also as far as I know ItemAdded event will be fired by sharepoint after the list item is added it does not depend on page post back.
    • Edited by Toms Edison Monday, July 28, 2008 8:03 PM modified
    Monday, July 28, 2008 7:59 PM
  •  I think you're wrong about ItemAdded firing after postback....If you have a development environment set up, put a break point on the ItemAdded, and then another one on PostBack.  You will see ItemAdded gets fired first...Same goes for any other Control's event handling.

    I do have another idea...Does your production environment have another web part on it?  I know if I update another control on a form, the postback gets fired for each web part that I have, and if you have more than one web part on the page it could cause the postback to fire more than once.  It all depends on what the other web parts are doing.  I had the same problem because I usually follow a naming convention when I create my controls, and two web parts had the same name so both of them were firing even though I only clicked on one.

    Just a thought....
    Thursday, July 31, 2008 11:44 AM
  • Thanks for your reply.

    I am sure there is only one webpart. I tried the same feature in Home site and subsites of the webapplication. Then the event fires only once. when i create a new site collection and activate the feature in it, then event fire twice. If i deactivate the feature its calling once(working fine). Then I deactivated the features in all other sites. Still its working fine. I dont get the clue where the event receiver is activated.

    Friday, August 08, 2008 11:31 AM
  • It would be great if you can paste in the feature Xml and your event handler code. This way we'll get to know what's exactly going on.
    Friday, August 08, 2008 7:38 PM
  • Hi,
    I ran into the same issue of getting event fired twice. When I looked into the event recievers registered for the list (SPList.EventRecievers) I found that Item added event was registered twice.
    Deactivate the event reciever by deactivating the feature (If you have used feature to register the recievers)

    And delete the duplicate event reciever.

    There are many event recievers, so make sure you delete the correct one.

    SPEventReceiverDefinitionCollection eventcoll = list.EventReceivers;
    SPEventReceiverDefinition eventtodelete = null;
                         foreach (SPEventReceiverDefinition eventdef in eventcoll)
                        {
                            if (eventdef.Name=="LibItemAdded")
                            {
                                 eventtodelete=eventdef;
                            }
                        }
                        if(eventtodelete!=null)
                        {
                             eventdef.Delete();
                        }


    Now reactivate the feature to register the event recievers.
    You can recheck if only one event is registered.


    Situations like this can happen if you make a list template from site where this events was activated.
    Friday, January 16, 2009 7:57 PM
  • I have a blog entry that goes into some detail on this scenario as well.
    http://blogs.msdn.com/mwasham/archive/2008/12/09/why-does-my-event-receiver-run-multiple-times.aspx

    Posting is provided "AS IS" with no warranties, and confers no rights.
    • Proposed as answer by Shafie_007 Monday, April 13, 2009 2:05 AM
    Friday, January 16, 2009 9:11 PM
  • Hi

    I have a very peculiar problem whenever a site is been created dynamically, the events with the custom list registered twice in a new site. i.e. ItemAdded registered twice with SequenceNumber 10000 & 10001 same goes for ItemUpdated event here i never used 10001 sequence number in the elements.

    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
      <Receivers ListTemplateId="100">
        <Receiver>
          <Name>MyListEvents</Name>
          <Type>ItemAdded</Type>
          <SequenceNumber>10000</SequenceNumber>
          <Assembly>MyListEvents, Version=1.0.0.0, Culture=neutral, PublicKeyToken=862d210841e66ec1</Assembly>
          <Class>MyListEvents.Events</Class>
          <Data></Data>
          <Filter></Filter>
        </Receiver>
         <Receiver>
          <Name>MyListEvents</Name>
          <Type>ItemUpdated</Type>
          <SequenceNumber>10000</SequenceNumber>
          <Assembly>MyListEvents, Version=1.0.0.0, Culture=neutral, PublicKeyToken=862d210841e66ec1</Assembly>
          <Class>MyListEvents.Events</Class>
          <Data></Data>
          <Filter></Filter>
        </Receiver>
      </Receivers>
    </Elements>

     
    I have a site template saved in the root web, whenever a new list item is created a new site based on saved template is created. In the site template we have a custom list that has attached 2 events ItemAdded and ItemUpdated and its been activated before saved as template. To double confirm whether my custom event handler registered twice i used SPSTips Utility Pack to confirmed its been registered only once in the site template. But whenever a new site is created based on this template, the events are registered twice. 

    Steps i have followed to register an event:

    1. Register new assembly in GAC
    2. Install feature
    3. Activate feature
    4. run iisreset

    I did redo all the stuff with new assembly and with new feature just in case with old assembly but it still persists to register twice with sequence number which i haven't registered with. Any idea why events getting registered twice when a site is created programatically?



    Shaf
    Monday, April 13, 2009 2:21 AM