How To Install SharePoint Event Handler To A List

This is an example of how to install or uninstall an event handler to/from a SharePoint list or document library.

Reference
Microsoft.Sharepoint (C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI\Microsoft.SharePoint.dll)

To Install an event handler from a SharePoint list
string assemblyName = "My.Assembly.Name, Version=1.0.0.0, Culture=neutral, PublicKeyToken=XXXX"; string className = "My.Class.Name"; // This class is supposed to inherit SPItemEventReceiver SPSecurity.RunWithElevatedPrivileges(delegate {    using (SPSite site = new SPSite("http://some.sharepoint.site"))     {         using (SPWeb web = site.OpenWeb)         {             SPList myList = web.Lists["Some List Name"];             myList.EventReceivers.Add(SPEventReceiverType.ItemAdded, assemblyName, className);         }     }    });

To Uninstall an event handler from a SharePoint list
string assemblyName = "My.Assembly.Name, Version=1.0.0.0, Culture=neutral, PublicKeyToken=XXXX"; string className = "My.Class.Name"; // This class is supposed to inherit SPItemEventReceiver SPSecurity.RunWithElevatedPrivileges(delegate {    using (SPSite site = new SPSite("http://some.sharepoint.site"))     {         using (SPWeb web = site.OpenWeb)         {             SPList myList = web.Lists["Some List Name"];             for (int i = 0; i < myList.EventReceivers.Count; i++)             {                 if (myList.EventReceivers[i].Assembly == assemblyName && myList.EventReceivers[i].Class == className)                 {                     myList.EventReceivers[i].Delete;                     break;                 }             }         }     }    });