Introduction To SharePoint Event Receivers

Here, I will explain what is Event Receiver and what the main purpose of Event Receiver in SharePoint is.


Event Receiver is an important feature of SharePoint. The use of Event Receiver is to handle the events. It is like a method that is triggered when an action occurs on a specified SharePoint object. Triggering actions have some activities including,
  1. Adding
  2. Updating
  3. Deleting
  4. Moving
  5. Checking In
  6. Checking Out.
We may need to perform some actions like notifying the person who created the list item, or deleted it. By using Event Receivers, we can easily maintain the secured data in SharePoint.


There is a Document Library and the full permission has been given to your Manager. So, we need to configure that document library in a manner that if anyone is trying to add a document in a library, your manager will get an alert like "someone is trying to add documents in library. We need you approval. " Once your Manager approves, then only the user can add a document in library.

Once a user has added a document in a library, an auto-generated message will be sent to your manager. At this time, we require Event Receiver to do this thing.

Types of Event Receiver

There are two types of Event Receiver in SharePoint,
  1. Synchronous Event Receiver
  2. Asynchronous Event Receiver
Synchronous Event Receiver or Before Event Receiver

Before events fire before the corresponding event action occurs and before SharePoint has written any data to the content database.

Asynchronous Event Receiver or After Event Receiver

After events fire after the event action has completed and after SharePoint has written to the content database to commit the event action. After events do not support cancelling the event action. A common example is sending out email notifications to let all the members of a site know when a new document has been uploaded.

Classes of SharePoint Event Receiver
  • Base class: Microsoft.SharePoint.SPEventReceiverBase.
  • Base class for List Item: Microsoft.SharePoint.SPItemEventReceiver
  • Base class for SharePoint List: Microsoft.SharePoint.SPListEventReceiver
  • Base class for Email: Microsoft.SharePoint.SPEmailEventReceiver
We can use the event to perform the following activities
  • Validate the Item
  • Log the information
  • Create associated items
Event Receiver Base Classes
  • SPItemEventReceiver
  • SPListEventReceiver
  • SPEmailEventReceiver
  • SPWebEventReceiver
  • SPWorkflowEventReceiver
How to create Event Receiver in SharePoint


Open Your Visual Studio and create a new Project.


On the File menu, point to Open, and then click SharePoint 2010 Project/Solution.



Choose the option where you want to deploy your solution.



Next , you have to choose which type of Event you want and what type of event you require.



Once you click on Finish button, you will be redirected to the code view. Here, the base class will be created automatically.
  1. public class EvtRcv__AddList: SPItemEventReceiver {  
  2.     ///  
  3.     /// An item is being added.  
  4.     ///  
  5.     public override void ItemAdding(SPItemEventProperties properties) {  
  6.         base.ItemAdding(properties);  
  7.         using(SPSite site = new SPSite("")) {  
  8.             using(SPWeb web = site.OpenWeb()) {  
  9.                 if (properties.ListTitle == "TestList") {  
  10.                     SPList list = web.Lists["TestList"];  
  11.                     SPListItem item = list.Items.Add();  
  12.                     string Room = properties.AfterProperties["Room"].ToString();  
  13.                     web.AllowUnsafeUpdates = true;  
  14.                     web.Lists.Add("Calender_" + Room, "This list was created programmatically", web.ListTemplates["Calendar"]);  
  15.                     properties.AfterProperties["RelatedRoom"] = "Calender_" + Room;  
  16.                     item["RelatedRoom"] = properties.AfterProperties["RelatedRoom"];  
  17.                     web.Update();  
  18.                     web.AllowUnsafeUpdates = false;  
  19.                 }  
  20.             }  
  21.         }  
  22.     }  
  23. }  

We are now all set to deploy our Event Receiver. To build and deploy, just press “F5”.


Now, you go to your site settings and select Manage Site Features under site actions.


Now, you can see your Event Receiver inside your Site Collection. You can use the same steps to implement Sandbox solution to develop this.


How to cancel the actions using Event Receiver?

SharePoint before event (synchronous) can be cancelled since it get trigged before completing the operation. Any validation can be applied as well as the event can be cancelled using event properties.

  1. public override void ItemAdding(SPItemEventProperties properties) {  
  2.     base.ItemAdding(properties);  
  3.     if (condition = true) {  
  4.         properties.Cancel = True;  
  5.     }  
  6. }  

ListUrl works only if feature has Scope=”Web”. In case the feature has Scope=”Site”, event receiver is fired for every list and library, ListUrl is ignored.

PowerShell Script to Deploy your solution

Add-SPSolution full path of the solution file

Activate the solution: Install-SPSolution solution-name.wsp –GACDeployment –AllWebApplications

Active the feature: Enable-SPFeature FeatureFolderName -Url http://server/site/subsite


Can SharePoint events be cancelled and redirected to different page?

Ans - Yes

  1. public override void ItemAdding(SPItemEventProperties properties) {  
  2.     base.ItemAdding(properties);  
  3.     properties.Cancel = true;  
  4.     properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;  
  5.     properties.RedirectUrl = "/_layouts/DeletingEventReceiver/ErrorPage.aspx";  
  6.     this.EventFiringEnabled = false;  
  7. }  

By default, Event Receivers are asynchronous - they run under the SharePoint Timer Service (OWSTIMER.exe). 

Rotary International Infotech Pvt Ltd
Rotary International is an international service organization whose stated purpose is to bring toget