[News! I just released my album Songs From The Wounded Heart! Want to download a free song from it and also hear a song from my next album? Sign up here today to get the free songs!]
So for the past week or so I’ve had this looming issue hanging over my head that I would try to nail down in-between other issues that I swear was going to be the death of me. I knew whatever it was had to be something really simple that I kept overlooking and something that, when I finally figured it out, would be quite elemental.
The problem was that I had written an EventReceiver to handle ItemAdded events for a custom list. The handler simply creates a task in the Tasks list corresponding to the new item added to the custom list. Every time an item was added to the custom list, the event was firing twice. I was associating the event receiver to the custom list through FeatureActivated on a feature receiver for my Event Receivers project.
I had already covered all the basic things such as:
- Ensuring I set EventFiringEnabled = false
- Ensuring that Require Check Out wasn’t the cause
- Ensuring that my handler code wasn’t actually adding back to the list
- No, the event receiver association wasn’t applied via a site template. This was a UI created site, and the list was not generated from a template either.
I even went as far as completely emptying the body of the handler to see that in fact the event was firing twice.
As with any problem, I always try to resolve it through the process of elimination by first isolating the core issue into a test project and therefore out of the primary project I’m experiencing it in because sometimes you can’t see the forest because of the trees. This particular problem was no exception.
After creating the simple project, the problem was still there.
Even a simple app to enumerate the event receivers associated to the list only showed the one instance. It was crazy.
Well, I had moved on to other things again, when all of a sudden, I noticed that new tasks were being created in the task list for some documents that I was uploading to a different document library. Aha! Now I had something else to go on. The event wasn’t firing twice for the same list, it was firing for ALL Lists. That’s pretty much the symptom for a receiver associated to a list template, but this was NOT a list template!
Then it dawned on me.
I took another look at my project.
I took another look at my event receiver I had added via Visual Studio to the project.
Then I realized the mistake I had made.
Yes, I had left the elements file for the Event Receiver there, and therefore when the feature was activated for the project, the receiver got associated to ALL lists on the site. It wasn’t going to show up in the EventReceivers collection for that list instance, because it was associated to the list type. When I added it to the list instance, it just caused the event to fire twice on THAT list. <facepalm>
I could have just removed the elements file reference from the feature, but just chose to delete the elements file in its entirety. I didn’t need it.
So folks, when you create an event receiver via the SharePoint tools in Visual Studio, and you REALLY don’t want it associated to every single list, remember not to be a bone head like me.
I knew the problem was elemental, I just didn’t realize how elemental it was.