Earlier today, I was working on a custom survey list event receiver that ports results into a SQL database table when I noticed some peculiar behaviour: Every time I added an entry to the survey, two duplicate rows were added to the SQL table.
Poking around the web, I see that this is indeed a common issue – David Birin captured the problem (and a solution) quite succintly on his blog in his January 2009 entry. As he notes, the root of the problem is that if you have your event receiver deployed as a Feature (I did) and then create a template of a targeted list (you bet I did that) then the event receiver registrations are “baked in” to the template definition.
Now, when you create a list based on this template in a site that has the same event receiver Features enabled, you now are running each event handler twice. As you repeat this process, you register more event receivers and things get out of hand geometrically.
NOT GOOD.
A Hack for a Solution
David’s solution is to add in some custom code that loops through the event receivers and deletes them individually. While this is robust for future-proofing, I wanted an easier way so that I could get back to coding. Here’s what I did:
- First, I went to the List Template Gallery for my Site Collection and saved the offending list template to my C:\ drive and renamed the extension from “.stp” to “.cab”
- Next, I opened up the .cab file and extracted the manifest.xml file within and tossed it into Visual Studio where I cleaned it up with an Edit -> Advanced -> Format Document
- I located the <Receivers> element and removed each <Receiver> child element.
- I changed the <TemplateTitle> to reflect the new version, eg. “My Survey V4”
- I made similar changes to the <Form> and <WebPart> element URL attributes, eg. “Lists/My Survey V4/DispForm.aspx”
- Next, I saved this file and then used MAKECAB.EXE to package it up (comes with the Windows SDK) and renamed the extension from “.cab” to “.stp”
- I opened up a browser on my test server, navigated to the List Template Gallery and uploaded my revised template and created a list from it.
- Presto! The ItemAdded event was only firing once as it should.
Hope this helps anyone encountering the same issue – it’s a quick-fix which helps avoid the ground-zero for the issue which is building a template of a list while the event receiver feature that targets that list is enabled.
f04121ea-6f8e-4013-b78a-b0b6fcb79dd5|0|.0