//assembly name of the enevnt handler class
string asmName = "EventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=bdfdd9c76bf90bef";
//event handler class
string eventClass = "EventHandler.EventHandler";
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb web = (SPWeb)properties.Feature.Parent;
SPList testList = web.Lists["test"];
testList.EventReceivers.Add(SPEventReceiverType.ItemUpdated, asmName, eventClass);
testList.EventReceivers.Add(SPEventReceiverType.ItemUpdating, asmName, eventClass);
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPWeb web = (SPWeb)properties.Feature.Parent;
SPList testList = web.Lists["test"];
Guid eventReceiverGuid = Guid.Empty;
foreach (SPEventReceiverDefinition receiverDef in testList.EventReceivers)
{
if (receiverDef.Assembly == asmName)
{ eventReceiverGuid = receiverDef.Id; }
}
if (eventReceiverGuid != Guid.Empty)
{ testList.EventReceivers[eventReceiverGuid].Delete(); }
}Sunday, January 10, 2010
Add and Remove List Event Handlers with a Feature in C#
Adding event handlers with C# in a feature is sometimes the only way to go. If you want to add an event handler to a single list instance and not a list or content type you have to add it with C#. The code to add event handlers is pretty straight forward and can be seen below.
Labels:
C#,
Event Handler,
Feature,
SPList
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment