When you disable event firing, it should not be kept disabled

Published 05-01-2019 03:28 PM 87 Views
Microsoft
First published on TECHNET on Jun 06, 2012

This post is a contribution from Nishand Vasudevan, an escalation engineer with the SharePoint Developer Support team.

Disabling the event receivers can stop the workflow events.  For example, disabling the event can stop workflow from starting and could stops firing events like OnTaskChanged.

The below code will not trigger OnTaskChanged event that otherwise would.  The code snippet is to depict what you shouldn’t be doing in your code and it’s an illustrative example.

1: public class EventFiringHandler : SPItemEventReceiver


2: {


3: public void DisableHandleEventFiring()


4: {


5: this.EventFiringEnabled = false;


6: }


7:


8: public void EnableHandleEventFiring()


9: {


10: this.EventFiringEnabled = true;


11: }


12: }





1: EventFiringHandler handler = new EventFiringHandler();


2: SPSite mysite = new SPSite("http://localhost/");


3: SPWeb web = mysite.OpenWeb();


4: SPList list = web.Lists["Tasks"];


5: SPListItem item = list.GetItemById(<taskItemID>);


6: Hashtable hashT = new Hashtable();


7: hashT.Add("Title","MyNewTitle");


8: handler.DisableHandleEventFiring();


9: SPWorkflowTask.AlterTask(item, hashT, true);


Hope this helps!






%3CLINGO-SUB%20id%3D%22lingo-sub-509659%22%20slang%3D%22en-US%22%3EWhen%20you%20disable%20event%20firing%2C%20it%20should%20not%20be%20kept%20disabled%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-509659%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3E%20First%20published%20on%20TECHNET%20on%20Jun%2006%2C%202012%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CP%3EThis%20post%20is%20a%20contribution%20from%20Nishand%20Vasudevan%2C%20an%20escalation%20engineer%20with%20the%20SharePoint%20Developer%20Support%20team.%3C%2FP%3E%0A%20%20%3CP%3EDisabling%20the%20event%20receivers%20can%20stop%20the%20workflow%20events.%26nbsp%3B%20For%20example%2C%20disabling%20the%20event%20can%20stop%20workflow%20from%20starting%20and%20could%20stops%20firing%20events%20like%20OnTaskChanged.%3C%2FP%3E%0A%20%20%3CP%3EThe%20below%20code%20will%20not%20trigger%20OnTaskChanged%20event%20that%20otherwise%20would.%26nbsp%3B%20The%20code%20snippet%20is%20to%20depict%20what%20you%20shouldn%E2%80%99t%20be%20doing%20in%20your%20code%20and%20it%E2%80%99s%20an%20illustrative%20example.%3C%2FP%3E%0A%20%20%3CDIV%20id%3D%22codeSnippetWrapper%22%3E%0A%20%20%20%3CDIV%20id%3D%22codeSnippet%22%3E1%3A%20public%20class%20EventFiringHandler%20%3A%20SPItemEventReceiver%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%202%3A%20%7B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%203%3A%20public%20void%20DisableHandleEventFiring()%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%204%3A%20%7B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%205%3A%20this.EventFiringEnabled%20%3D%20false%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%206%3A%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%207%3A%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%208%3A%20public%20void%20EnableHandleEventFiring()%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%209%3A%20%7B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%2010%3A%20this.EventFiringEnabled%20%3D%20true%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%2011%3A%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%2012%3A%20%7D%20%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippetWrapper%22%3E%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippet%22%3E%3CBR%20%2F%3E%201%3A%20EventFiringHandler%20handler%20%3D%20new%20EventFiringHandler()%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%202%3A%20SPSite%20mysite%20%3D%20new%20SPSite(%22http%3A%2F%2Flocalhost%2F%22)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%203%3A%20SPWeb%20web%20%3D%20mysite.OpenWeb()%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%204%3A%20SPList%20list%20%3D%20web.Lists%5B%22Tasks%22%5D%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%205%3A%20SPListItem%20item%20%3D%20list.GetItemById(%3CTASKITEMID%3E)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%206%3A%20Hashtable%20hashT%20%3D%20new%20Hashtable()%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%207%3A%20hashT.Add(%22Title%22%2C%22MyNewTitle%22)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%208%3A%20handler.DisableHandleEventFiring()%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%209%3A%20SPWorkflowTask.AlterTask(item%2C%20hashT%2C%20true)%3B%20%3CBR%20%2F%3E%3C%2FTASKITEMID%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%20Hope%20this%20helps!%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-509659%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20TECHNET%20on%20Jun%2006%2C%202012%20This%20post%20is%20a%20contribution%20from%20Nishand%20Vasudevan%2C%20an%20escalation%20engineer%20with%20the%20SharePoint%20Developer%20Support%20team.%3C%2FLINGO-TEASER%3E
Version history
Last update:
‎Aug 27 2020 03:31 PM
Updated by: