What Are Event Receivers in SharePoint?

SharePoint Event Receivers are a feature that helps developers to execute custom code when some specific events occur. These events can be user-driven actions, such as adding a new item, or system-level events, such as installing SharePoint add-ins. The process of implementing event receivers involves writing code to respond to these events, and these codes need to be registered within the SharePoint environment and then they can be invoked when the specific events occur.

SharePoint
SharePoint

Event Receivers Types and Application

There are two main types of event receivers in SharePoint: synchronous and asynchronous.

  • Synchronous event receivers, also known as "before" event receivers, execute before the event completes, allowing you to cancel the action if necessary.
  • Asynchronous event receivers, or "after" event receivers, execute after the event has been completed, suitable for post-processing tasks.

SharePoint Event Receivers have wide-ranging applications, including but not limited to the following scenes:

  • User-driven operations: Workflows triggered when items are added, edited, or deleted from lists.
  • System-level events: Automation tasks triggered during the installation or uninstallation of SharePoint add-ins.
  • Information integration: Real-time integration of information from different systems into SharePoint, enabling enterprise-level information sharing and business process optimization.

How to Use Event Receivers in SharePoint

To use event receivers in SharePoint, you need to take the following several steps:

Part 1. Create an Event Receiver Class

This involves defining a class that inherits from one of the event receiver base classes provided by SharePoint. The base classes you can inherit from include SPItemEventReceiver, SPListEventReceiver, SPWebEventReceiver, and SPFeatureReceiver, depending on the scope of the events you want to handle. For example, to handle item-level events, you would inherit from SPItemEventReceiver.

public class CustomItemEventReceiver : SPItemEventReceiver {     // Constructor     public CustomItemEventReceiver() { }

    // Define event methods here }

Part 2. Override Event Methods

Customize the behavior by overriding specific event methods, such as ItemAdded, ItemUpdated, or ItemDeleted. These methods are called when the corresponding events occur. For instance, if you want to perform an action when an item is added to a list, you would override the ItemAdded method.

public override void ItemAdded(SPItemEventProperties properties) {     base.ItemAdded(properties);

    // Custom logic here     SPListItem item = properties.ListItem;     item["Title"] = "New Title"; // Modify the item's title     item.Update(); // Save changes }

public override void ItemUpdated(SPItemEventProperties properties) {     base.ItemUpdated(properties);

    // Custom logic here }

public override void ItemDeleted(SPItemEventProperties properties) {     base.ItemDeleted(properties);

    // Custom logic here }

Part 3. Deploy the Event Receiver

Deploy the event receiver to your SharePoint environment, typically through a SharePoint solution package (WSP) or via SharePoint Designer. Here’s a more detailed explanation of the deployment process:

  • Create a SharePoint Solution Package (WSP): Use Visual Studio to create a new SharePoint project. Add your event receiver class to the project. Configure the feature that will deploy your event receiver to the appropriate list or site.

            CustomItemEventReceiver         ItemAdded         10000         $SharePoint.Project.AssemblyFullName$         Namespace.CustomItemEventReceiver  

  • Build and Package: Build your project in Visual Studio, and package it into a WSP file.

  • Deploy the WSP to SharePoint: Use PowerShell or Central Administration to deploy the WSP file to your SharePoint farm.

    Add-SPSolution -LiteralPath "C:\Path\To\YourSolution.wsp"Install-SPSolution -Identity "YourSolution.wsp" -WebApplication "http://yoursharepointsite" -GACDeployment 
    
  •  Activate the Feature: Once the solution is deployed, activate the feature to associate the event receiver with the appropriate list or site.

Enable-SPFeature -Identity "YourFeatureName" -Url "http://yoursharepointsite"

  • Via SharePoint Designer: Alternatively, you can use SharePoint Designer to attach your event receiver to a list. Open your site in SharePoint Designer, navigate to the list settings, and add your event receiver under the "Event Receivers" section.

How to Configure Event Receivers in SharePoint?

Setting
Setting

Configuring event receivers in SharePoint involves:

  • Register the Event Receiver: Associate the event receiver with a specific list, library, or content type.
  • Set Up Permissions: Ensure that the event receiver has the necessary permissions to perform its tasks.

When configuring event receivers, consider the following best practices:

  • Accessibility of Service Endpoints: The service endpoint implementing the event receiver must be accessible to anonymous users. This is necessary because event receivers need to handle operations driven by end users, such as ItemAdded.

  • Handling Access Tokens: If the event receiver is operated from an application context using app-only access tokens, and operations are user-driven, adding the event receiver to the host web will return an access token. For host webs without application context, the event receiver will not return an access token and must use app-only access tokens to access the host web.

  • Timeouts and Retry Mechanisms: The AppInstalled event must complete within 30 seconds, otherwise SharePoint considers it failed. Regular events (e.g., ItemAdded) also have a 30-second timeout but lack retry mechanisms.

  • Configuration of Event Receivers: Event receivers can only be added to the host web through CSOM/REST API. SharePoint will call the event receiver endpoint configured for each specific event, but it cannot guarantee that the code within the endpoint will execute because this code does not run on the SharePoint server.

  • Conditions for Code Execution: If the event receiver endpoint URL is unavailable, the event receiver code will not execute. The URL may be unavailable for various reasons.

  • Scenarios for Using Event Receivers: Event receivers are effective not only for list items but also for triggering events on SharePoint, such as lists and web sites, which is a good way to handle post-operation tasks in SharePoint.

  • Ability to Read Configuration Files: Like web parts, event receivers can read configuration files because some information is flexible and variable. Configuration files can be written in the Web.config of the related application, and then read using .NET methods.

The above is all about Event Receiver in SharePoint. Here, we also would like to share you with a multiple cloud manager, which supports SharePoint and other popular cloud drives including Google Drive, iCloud, OneDrive, Dropbox, etc. Apart from allowing you to perform basic operations to manage cloud data: download, upload, delete, rename and so on, it also enables you to directly transfer, sync and backup data across different cloud services or accounts.

  • Cloud Transfer: Move one cloud data to another without download and re-upload.
  • Cloud SyncSync data across clouds seamlessly in real time.
  • Cloud Backup: Dynamically backup and restore files between clouds.
  • Automatic: Set up a schedule to make task automatically run as preferred.
  • Offline: Transfer, sync or backup cloud data online without going through local.
  • Save Email to Cloud: Convert email (attachments) as PDF and save to cloud.
  • Save Image to Cloud: Mass download images on instagram or other sites to cloud.