SyntaxHighlighter

Showing posts with label Event Receiver. Show all posts
Showing posts with label Event Receiver. Show all posts

Tuesday, April 29, 2014

How to disable event firing in custom code


Recently I worked on application page which updates list item, which in turn causes item update event to fire. The goal was to avoid any event execution. I have done this many times before, but had a hard time recalling. This post is a note to myself, so I know where to look for code sample, if I forgot again. :)

If I am writing event receiver, I can use EventFiringEnabled property to disable events temporarily. But in application page I don’t’ have access to SPEventReceiverBase
which exposes this property. So here is the coed I used to temporarily disable event firing.

public class DisableItemEventFiring : SPItemEventReceiver, IDisposable 
{
public ItemEventFiring()
{
this.EventFiringEnabled = false;
}

public void  Dispose()
{
this.EventFiringEnabled = true;
}
}


Now we have defined class for disabling list item event, I can use this class in application page,

using (DisableItemEventFiring disableEvt = new DisableItemEventFiring ())
{
//no event will be fired
item.SystemUpdate();
}

//event firing enabled again
item.SystemUpdate();


Hope this helps.

-Javed

Friday, February 5, 2010

Validating document approval and sending email notification


Today I was working on a requirement to validate document approval (we are using OOTB document approval feature) based of some external business rules.

Requirement:
If business validation fails, I have to cancel document Approval process and if validation succeeds then I have to generate email notifying team with links to approved document.

Solution:
My first option was to use Workflow using Visual Studio (I have to re-use the functionality for many other document libraries), but soon found out it's not possible to retrieve before and after values inside Workflow and to cancel list item update. Please let me know if you think otherwise. The other option was to use Event Receiver which I followed. I override synchronous ItemUpdating event and again struggled to retrieve after "Approval Status" value. After couple of hours of debugging, I found key named vti_doclibmodstat in AfterProperites collection containing integer values 0, 1 or 2 which is nothing but Approved, Rejected or Pending statuses. So here is the complete code to retrieve before and after "Approval Status" inside event handler:

public override void ItemUpdating(SPItemEventProperties properties)
{
base.ItemUpdating(properties);
SPModerationStatusType beforeStatus = properties.ListItem.ModerationInformation.Status;
SPModerationStatusType afterStatus = 
(SPModerationStatusType)Int32.Parse(properties.AfterProperties["vti_doclibmodstat"].ToString());
if ((beforeStatus != afterStatus) && 
(afterStatus == SPModerationStatusType.Approved))
{
if (Validate(properties) == false)
{
properties.Cancel = true;
properties.ErrorMessage = "";
}
else
{
SendEmail(properties);
}
}
}

And now I can validate document approval process, cancel approval process and send email notifications.

Hope this helps.
-Javed