Ok, well I think I’ve worked out why the GUId isn’t getting defined.
The code I’ve written creates an EventHandler on the base content type. When this content type gets assigned to a library, the content type is instanced to the library and as a result I think the GUID i assign is removed.
I think were I to assign an event handler directly to a list, then this wouldn’t be the case and I could give a GUID in the creation method.
I’ve not tested this yet, but it seems to make sense.
Paul.
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;
deletingReceiver.Update();
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..
Paul.