[Important News!]
I’ve kicked off a new project/band where I’m creating music with a heavier techno/metal sound called binarywaste. You should check it out by clicking right here!
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.
– Keith
I have been scratching my head about this for a while and it was the same problem!
Was being attached to my list via FeaturedActivated and also all lists via the elements.xml
Thank you!
Lol elemental!!
Thanks for this post, I only just came across this issue, saved me heaps of time!
Is this a 2010 issue only? I have a similar set up in 2007 and have not encountered this issue. I just confirmed it by checking all of the lists and libraries in my site and the event receiver is bound to just the one that my feature receiver activated. I have an elements.xml in both the feature receiver project and the event receiver project.
Jason, is the elements file referenced from the feature? Probably not. This was the glue that made this problem happen.
I checked both projects and the elements are referenced in both. I use WSP Builder, so the structure is already in place by default.
Your post has me concerned that I’ve maybe overlooked something. In my feature receiver project code, I am adding the event receiver assembly as a new SPEventReceiverDefinition. I am not adding the receiver to all list types using a template ID. Is that the difference? I just don’t get how it is getting added to all lists in your example if you are using a two feature model like I am.
I’m too frustrated with the issue and did all nasty checks that you mentioned, i’m following the similar approach, its just with a feature receiver, i’m attaching ItemUpdating event to a document content type which was created using Browser. What could be the issue??
Thanks for your explication.
This helped me out in development, but when I deploy the wsp to the test environment, it’s still firing twice. (The element-file is nowhere to be found)
Thanks for any thoughts on this!
if (properties.ListTitle == “MyList”)
{
//do stuff
}
Thats what I did anyway
yes, that will work, but when you have many lists, and indeed many 100’s of items firing in each list it is not really efficient is it.
Sure, I never said it was the right way, it was just how i did it!
My question would be though, how do you the the event receiver to a particular list when you create it ?