Microsoft SharePoint: What are Event Receivers?

by | Jun 24, 2015 | BlogPosts, Tech Tips | 0 comments

Overview

An event receiver in Microsoft SharePoint is a method that is triggered when action occurs on a specified SharePoint object. Triggering events include actions such as adding, updating, deleting, moving, checking in, and checking out. The SharePoint object that listens to an event is known as event receiver hosts. Some of the event receiver hosts include site collections, sites, lists, and workflows.

Types of Event Receivers

There are two types of event receivers:

Synchronous event receivers: These events fire before an action occurs and before SharePoint has written any data to the content database. They always end with ‘ing’, such as: ItemAdding, ItemUpdating, etc. For example, ItemAdding event will fire when trying to add an item to a list; you can use this for performing data validation. These events support cancelling of event action. These events are also known as Before events.

Asynchronous event receivers:  These events fire after the event action has completed and after SharePoint has written to the content database to commit the event action. These events are recognisable as always ending with ‘ed’ like ItemAdded, ItemUpdated etc.

For example, ItemUpdated event fires after SharePoint updated the content database. You can use this to develop code that executes a logic that occurs after the item is updated. These events do not support cancelling of event action. These events are also known as After events.

Where to use Event Receivers?

You can use event receivers to trigger custom code whenever a certain operation takes place and to add behaviour to the events that occur within SharePoint. Below is a list of some of the objects and events on which you can trigger custom code:

Event host type

Supported events

Site collection

Deletion

Web

Creation

URL modification

Deletion

List

Creation

Deletion

Received an e-mail message

Field

Creation

Update

Deletion

Item

Creation

Update

Deletion

Check in

Check out

File movement

File conversion

Adding attachment

Deleting attaching

 

Developing using Visual Studio

An event receiver can be created in Visual Studio using the SharePoint project template. Below are the step to create a simple ItemUpdated event receiver.

  1. Open Visual Studio 2012 and create a new Project. Select SharePoint 2010 project template.2015-06-24 Blog - MS SP Event Receivers - Image 1
  2. Next, in the SharePoint Customization Wizard, choose Deploy as a farm solution and specify the site URL where you want the event receiver to be deployed. 2015-06-24 Blog - MS SP Event Receivers - Image 2
  3. Add a new item to the project. Right click project and select Add -> New Item  -> Event Receiver. 2015-06-24 Blog - MS SP Event Receivers - Image 3
  4. Next, in Event Receiver Settings dialog select the “List Item Events” as a type of event receiver and “Document Library” in the event source drop down. Select “An Item was updated” from Handle the following events and click Next. 2015-06-24 Blog - MS SP Event Receivers - Image 4
  5. Next, write some quick code to update a list item field.2015-06-24 Blog - MS SP Event Receivers - Image 5

Developing Event Receiver

Event receivers can be deployed/registered directly from Visual Studio or the process can be scripted using PowerShell. Following sections describes the steps for both scenarios.

Visual Studio

  • In Visual Studio, right click on the project and select Deploy. The solution is deployed to the farm and the feature is activated.

PowerShell Script

  • In Visual Studio, right click on the project and select Publish to file system. The solution file .WSP will be saved to the specified location.DeveDeve.
  • Use the following PowerShell commands to upload the solution to the farm solution store:

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, this will register the event receiver to all the libraries in the site collection.

You can bind or register an event receiver to specific lists or libraries by using the following PowerShell commands.2015-06-24 Blog - MS SP Event Receivers - Image 6

 

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *