Posts

Showing posts with the label Event Receivers

SharePoint 2010: Add an Event Receiver in code

Some requirements demand the adding of Event Receivers in code and not as features. Fortunately, there is a simple method to perform this task. The following code adds an 'ItemAdding' event receiver in the class 'MyEventReceiver'. The project namespace is 'MyProject' using (SPSite site = new SPSite("http://blah.com")) {    using (SPWeb web = site.OpenWeb())    {       SPList list = web.Lists["MyList"];       list.EventReceivers.Add(SPEventReceiverType.ItemAdding, Assembly.GetExecutingAssembly().FullName, "MyProject.MyEventReceiver");    } }

Disable all event receivers in SharePoint 2010

I found the following page( here  ) about disabling global event receivers. A fantastic post that solves a very specific need. The crux of the solution is as follows: public class MyClass {   SPListItem i = SPContext.Current.Web.Lists["Blah"].Items[0]   HandleEventFiring handleEventFiring = new HandleEventFiring();   handleEventFiring.DisableHandleEventFiring();   try   {      i.Update();   }   finally  {     handleEventFiring.EnableHandleEventFiring();   } } public class HandleEventFiring : SPItemEventReceiver {        public void DisableHandleEventFiring()       {           this.EventFiringEnabled = false;        }        public void EnableHandleEventFiring()       {   ...