Sunday, April 13, 2014

SharePoint 2013 - Event Receivers Overview

Overview
Event receivers are classes that allow you to respond to events that occur to SharePoint items such as lists or list items.

For example, if a project manager needs to be alerted when new documents or files are added to a given document library, you could write event receiver code to send an e-mail notification to the project manager, and then bind that code to the item-addition action on the document library.

Commonly used Event Receivers
1) SPItemEventReceiver: Event receivers related to single SharePoint items (SPListItem).
2) SPListEventReceiver: Event receivers for trapping events related to list instances.
3) SPWebEventReceiver: Event receivers that catch events raised by webs.
4) SPWorkflowEventReceiver: Event Receivers related to workflows instances.
5) SPEmailEventReceiver: Event receivers that trigger whenever an e-mail–enabled document library receives an e-mail message.

Operations available in Event Receivers

Object
Operations
Site collectionDeletion
WebCreation
URL modification
Deletion
ListCreation
Deletion
Received an e-mail message
FieldCreation
Update
Deletion
ItemCreation
Update
Deletion
Check in
Check out
File movement
File conversion
Adding attachment
Deleting attaching


Synchronous (Before) Events
The Before events are those that happen just after the event occurred, but before SharePoint writes any data to the SharePoint content database. Such events are useful for custom validation, rules checking, and cancelation of users’ actions.

Asynchronous (After) Events
The After events are those that happen after SharePoint commits the data to the SharePoint content database. These After event handlers cannot cancel the current action/operation, but they are guaranteed to execute only after specific actions.

Event Receiver Base Class
All of these receiver base classes (except for SPEmailEventReceiver and SPFeatureReceiver) inherit from a common base class named SPEventReceiverBase, which provides the very basic infrastructure for events management.
In addition, all of these classes provide a set of virtual methods that developers should override when implementing events.
i) Before events run in the same process and thread as the current action.
ii) After events by default run in a background thread; however, you can force them to run synchronously, that means within the same process and thread of the current action.

Event Binding
Event binding (also known as event registration) can be done by using the server object model or by using Feature XML. The following sections provide detailed information about binding events in SharePoint Foundation.

Event Cancellation
Event cancellation enables you to cancel an event receiver operation in a Before event prior to the conclusion of the action. For example, we can write some code in the ItemAdding event receiver that cancels the action of adding the item. So when the user tries to add an item, the operation is cancelled and the item is not added to the list. When the operation is cancelled, be sure to show the user an error message or provide a custom error message by redirecting the user to a specific URL.

Event Receiver Sequence
The event receiver sequence specifies the order in which an event receiver is executed in cases where an event triggers multiple event receivers. For example, if you have two ItemAdded event receivers bound to the same list (one from Assembly "1" and the other from Assembly "2"), the event receiver that is bound with a lower sequence number is executed first. A practical example is adding an event receiver to a system list that already has a system event receiver bound to it. In that case, you assign the new event receiver a higher sequence number.

Event receiver base classes and supported events
Following is the list of Event Receiver base classes and the events supported by them:
Event receiver base classAvailable event host typesSupported events
SPWebEventReceiverSPSiteSiteDeleting
SPWebSiteDeleted
WebAdding
WebProvisioned
WebDeleting
WebDeleted
WebMoving
WebMoved
SPListEventReceiver (lists)SPSiteListAdding
SPWebListAdded
ListDeleting
ListDeleted
SPListEventReceiver (fields)SPSiteFieldAdding
SPWebFieldAdded
SPListFieldDeleting
SPContentTypeFieldDeleted
FieldUpdating
FieldUpdated
SPItemEventReceiverSPSiteItemAdding
SPWebItemAdded
SPListItemDeleting
SPContentTypeItemDeleted
ItemUpdating
ItemUpdated
ItemFileConverted
ItemFileMoving
ItemFileMoved
ItemCheckingIn
ItemCheckedIn
ItemCheckingOut
ItemCheckedOut
ItemAttachmentAdding
ItemAttachmentAdded
ItemAttachmentDeleting
ItemAttachmentDeleted
SPEmailEventReceiverSPSiteEmailReceived
SPWeb
SPList
SPWorkflowEventReceiverSPSiteWorkflowStarting
SPWebWorkflowStarted
SPListWorkflowCompleted
SPContentTypeWorkflowPostponed

I hope this post will give you an overview about Event Receivers in SharePoint 2013! In the next post we see how to create an Event Receiver and bind it to a list.

Reference:
1) Microsoft SharePoint 2013 Developer Reference, by Paolo Pialorsi - Book.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.