Tuesday, August 5, 2008

Registering list item Event Handler

Scenario:
You want to add an event handler from a particular item within your list/library

Pre-requisite:
You already have assembly compiled

Code:

using Microsoft.SharePoint ;

SPSite site = new SPSite("http://moss");
SPWeb web = site.AllWebs["/"];
SPList list = web.Lists["List_Name"];

string assemblyName = "[full 4-part assembly name]";
string className = "Namespace.ClassName";

list.EventReceivers.Add(SPEventReceiverType.ItemAdded, assemblyName, className);
list.EventReceivers.Add(SPEventReceiverType.ItemUpdated, assemblyName, className);
list.EventReceivers.Add(SPEventReceiverType.ItemDeleting, assemblyName, className);
Use:
Console Application, At the time of Feature Activation

0 comments: