Event Handlers from code.

Eek.. June already.. I’ve been on my hols, come back and realised that I’ve been somewhat remiss in posting to my blog.

So without further ado, I’ll talk about my current expedition into the realm of SharePoint code. Todays task is EventHandlers from code.

Attaching Event Handlers from code is pretty easy, the SDK shows various methods all of which seem to do the trick. However, I want my Event Handler to have a specific GUID as it’s ID. The idea behind this is that my management application knows what the different event handler ID’s are in all the environments.

And this is where my plan has currently fallen apart.

I’m using the following code to add the event handler (after correctly checking that an event handler of the same name and assembly doesn’t already exist of course..)

if (!itemDeletingExists)
SPEventReceiverDefinition deletingReceiver = erdc.Add(new Guid("F649A610-2C83-4f11-B73F-A7D1656DEACB"));
deletingReceiver.Name = "ResourceEventsItemDeleted";
deletingReceiver.Type = SPEventReceiverType.ItemDeleting;
deletingReceiver.SequenceNumber = 10000;
deletingReceiver.Assembly = assm;
deletingReceiver.Class = nsp;
receiverAdded = true;

When you step through the code above, the event handler is added quite happily and the deletingReceiver.ID = the guid used in the constructor.

So further down my code we grab the collection of event handlers currently held by the content type and sure enough, there is our (in this case 3 of them) event handlers, sitting happily in the collection.

However, if you grab the EventReceiverDefinition for each of them, the .ID is all zeroes… Is this a bug or intended behaviour? Further investigations ongoing..


Leave a Reply

Your email address will not be published.


This site uses Akismet to reduce spam. Learn how your comment data is processed.