Tuesday, July 27, 2010

Adding Event Reciever Programmatically

I was wondering one evening how would you programmatically add an event reciever to a SharePoint list programmatically, and then it hit me.

Get the Event Reciever collection for the specific list and just add your own event reciever to that collection, as shown below:

SPEventReceiverDefinitionCollection eventRecievers = spRequestList.EventReceivers;

SPEventReceiverDefinition defFile = spRequestList.EventReceivers.Add();

defFile.Name = "Auto Provisioning - Mark Parker";
defFile.Type = SPEventReceiverType.ItemAdded;
defFile.SequenceNumber = 10000;
defFile.Assembly = "SP2010.MarkParker, Version, Culture, PublicKeyToken=";
defFile.Class = "SP2010.MarkParker.EventReciever";

defFile.Update();
spRequestList.Update();

I have now added the Item added event to the list in question. If you wish to add the any other event recievers to the list simply follow the same method and just change the Type of event.

Hope this helped you

No comments:

Post a Comment