Monday, September 3, 2012

ItemDeleting Event Not Firing

I have written many event receivers.  I have written them for just about every conceivable business use, and for some that are not so conceivable.
I am a MCPD, passing both tests in the 90% range.  I have written these receivers for SPS 2003, MOSS 2007, SharePoint 2010, and even on SharePoint 2013 Beta.

I'm kind of a big deal.

So, why, when I was writing such a mundane application as an event receiver, did one of the events not fire? I overrode the proper method.  I used the correct syntax.  I had no runtime exceptions, and everything built and deployed just fine.  During debugging, as this was a farm deployed solution, I attached to the correct w3wp.exe worker process application (if it would have been a sandboxed solution I would have attached to the user worker process application, SPUCWorkerProcess.exe).  I knew this, because the other events I overrode were firing and doing their jobs just fine.  BUT when it came to the delete events nothing...  What the heck?????????

After looking around and pulling out more than a little bit of hair, out of frustration I created a new solution with just the delete events.  This solution worked just fine.  No muss no fuss.  So, why would the very same code not work in one solution, but not in another???   I did a file comparison of the wsp files to find out what was up.

What happened was this...  When I initially created the project in Visual Studio, I checked the box for just the ItemUpdating event.  Nothing else.  What Visual Studio does with this setting is it adds in the Elements.xml a <Receiver> section in a <Receivers> block.  In that block is an element called <Type>.
What I had forgotten to do with my first solution was to add a <Type> element for my ItemDeleting event.  The Elements.xml file registers the receiver with SharePoint.  If it doesn't have a <Type> section for your events, it doesn't matter how much code you have written in as many overridden methods, the will not fire.
Now, the ItemDeleting event is a separate receiver event, thus it does need its own <Receiver> section.
I just copied my ItemUpdating receiver, and pasted it below.  Then I changed the name and the <Type>.  Everything else can remain the same.

After realizing my mistake, I added the proper <Type> elements to register my deleting events, and BLAMO, they fired just as they should have.
One little trick that may catch many people is that you may have to deactivate then re-activate your feature in order to register the new receiver section, and get the event to fire.  For whatever reason, SharePoint may not load the new XML, even after the IIS Application Pool is recycled.  It can be very annoying, ESPECIALLY if you are trouble shooting other reasons why your event is not firing.

It was a rookie mistake that I made.  Nearly every part of SharePoint development has two parts to it.  The code or assembly part, and the declarative XML part.  The code never works without the XML letting SharePoint know what's up.

One of the good things about software development is that the code will always find away to keep you humble.

No comments:

Post a Comment