Here, I will explain what is Event Receiver and what the main purpose of Event Receiver in SharePoint is.
Introduction
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,
- Adding
- Updating
- Deleting
- Moving
- Checking In
- 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.
Example
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,
- Synchronous Event Receiver
- 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
Step1
Open Your Visual Studio and create a new Project.
Step2
On the File menu, point to Open, and then click SharePoint 2010 Project/Solution.
Step3
Choose the option where you want to deploy your solution.
Step4
Next , you have to choose which type of Event you want and what type of event you require.
Step5
Once you click on Finish button, you will be redirected to the code view. Here, the base class will be created automatically.
- public class EvtRcv__AddList: SPItemEventReceiver {
-
-
-
- public override void ItemAdding(SPItemEventProperties properties) {
- base.ItemAdding(properties);
- using(SPSite site = new SPSite("http://raj.com/4493")) {
- using(SPWeb web = site.OpenWeb()) {
- if (properties.ListTitle == "TestList") {
- SPList list = web.Lists["TestList"];
- SPListItem item = list.Items.Add();
- string Room = properties.AfterProperties["Room"].ToString();
- web.AllowUnsafeUpdates = true;
- web.Lists.Add("Calender_" + Room, "This list was created programmatically", web.ListTemplates["Calendar"]);
- properties.AfterProperties["RelatedRoom"] = "Calender_" + Room;
- item["RelatedRoom"] = properties.AfterProperties["RelatedRoom"];
- web.Update();
- web.AllowUnsafeUpdates = false;
- }
- }
- }
- }
- }
Step6
We are now all set to deploy our Event Receiver. To build and deploy, just press “F5”.
Step7
Now, you go to your site settings and select Manage Site Features under site actions.
Step8
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.
E.g
- public override void ItemAdding(SPItemEventProperties properties) {
- base.ItemAdding(properties);
- if (condition = true) {
- properties.Cancel = True;
- }
- }
Note
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
Note
Can SharePoint events be cancelled and redirected to different page?
Ans - Yes
Example
- public override void ItemAdding(SPItemEventProperties properties) {
- base.ItemAdding(properties);
- properties.Cancel = true;
- properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
- properties.RedirectUrl = "/_layouts/DeletingEventReceiver/ErrorPage.aspx";
- this.EventFiringEnabled = false;
- }
Note
By default, Event Receivers are asynchronous - they run under the SharePoint Timer Service (OWSTIMER.exe).