Thursday, March 24, 2011

How to call DisableEventFiring not from SpEventReceiver

In one of my previous posts I have used my custom class to operate with permissions in the list. And one line there disabled the event raising even the class itself wasn't inherited from SPItemEventReceiver
                     SharepointHelper.DisableEventFiring();

public class SharepointHelper: SPEventReceiverBase
    {
        public void DisableEventFiring()
        {
            DisableEventFiring();
        }
        public void EnableEventFiring()
        {
            EnableEventFiring();
        }
    }

There are some rumors that is not going to prevent from firing the event if this is not used in the context of SpEventReceiver.... But practice proved - it does work.
  Here is the example of using it in the form page class:
protected override void OnSaved(EBTSimpleSaveButtonEventArgs e)
        {
            base.OnSaved(e);


SPLongOperationExecutionParams spLongOperationExecutionParams = new SPLongOperationExecutionParams() { LeadingHtml = "Updating incident", TrailingHtml = "Please wait while the incident is being updated", RedirectUrl = _redirectUrl };            
SharePointHelper.InvokeInSPLongOperationContext(spLongOperationExecutionParams, delegate()
            {
                SharePointHelper.DisableEventFiring();
                SharePointHelper.InvokeInSPListItemContext(new OperationContext() { SiteUrl = SPContext.Current.Web.Url, UseSystemAccount = true }, SPContext.Current.List.RootFolder.Url, SPContext.Current.ItemId,
                    delegate(SPSite spSite, SPWeb spWeb, SPList spList, SPListItem spListItem)
                    {
                        ItemHelper.ItemHelperMethod(spListItem);                       
                    });
                SharePointHelper.EnableEventFiring();
            });
        }