SharePoint 2013: How To Enable Remote Event Receivers With Existing List And Libraries

In this article you will learn how to enable remote event receivers with existing list and libraries in SharePoint 2013.

While working with Remote Event Receivers I came across a scenario where we needed to hook up the Event Handlers to any of the existing lists in a site.

For example we had a list called Products in which we needed to stop the users from deleting any list item.

This requirement can be achieved simply by deploying a Farm Solution containing the Event Receiver SPI hooked up with the Products List Item Deleting Event.

But what if we are not allowed to deploy any code solution in SharePoint Environment?

The simple answer to this complex question is to involve Remote Event Receivers and hook them up to the existing list which is a bit of a tricky task.

In this article we will see the steps to register Remote Event Receivers with the existing Lists and Libraries while utilizing our knowledge based on earlier articles in this series which can be reached at:

  1. SharePoint 2013: Remote Event Receivers
  2. SharePoint 2013: How to develop Remote Event Receivers for App Events
  3. SharePoint 2013: How to develop Remote Event Receivers for List And List Item Events
  4. SharePoint 2013 : Remote Event Receivers on List Item Event – Real Business Scenarios

Let's start with a Provider Hosted App that will register the Remote Event Receivers with the existing SharePoint Lists

Create “New Project”,

New Project

Select App for SharePoint as SharePoint Project Template,

App for SharePoint

App for SharePoint

Specify the URL of the Debugging Site, which is called Host Web in case of an App.

Select “Provider-Hosted” as App Hosting Model,

Provider-Hosted

Select “ASP.Net Web Forms Application” as Project Template for the Remote Web,

ASP.Net Web Forms Application

Enter Certificate Details as we did in earlier demos,

Certificate Details

Certificate Details

On successful creation of the Solution for the two projects should be added as shown below.

  1. “PH-Bind-Event-Receivers-With-Existing-List”: Represents the Code Base for the Provider Hosted App.
  2. “PH-Bind-Event-Receivers-With-Existing-ListWeb”: Represents the Code Base for the Remote Web.

Code Base

Select App Project and Press F4 to see the properties.

Under App for SharePoint Events set the Events that the App is required to respond to.

Here we would need only App Installed and App Uninstalling event to work on so we would enable only these two events by changing the value to “True”.

App

As soon as we enable the App Events, a new service class gets added to the Solution by the name of AppEventReceiver.svc under the Services Folder.

Services

Detailed walkthrough of the highlighted steps is as follows,

Step 1: Check for the Event Raised and make sure that we are handling only “AppInstalled” Event.

Step 2: This step is the trickiest of all. It is important to notice how we are getting the full path of the Service Class that will be listening to the lists events.

This is the Service that will be hosted somewhere outside SharePoint, and due to this the usual SharePoint Tokens will not work here which means “~remoteAppUrl” Url Token will not work in this case.

So in order to get the complete path to the Service Class we need to make use of “OperationContext” class.

The “OperationContext” class provides you methods (as highlighted below) to extract Absolute Uri to the Service Class irrespective of where it is hosted.

Step 3: Retrieve the Remote Web URL to get the fully qualified path of Service Class.

Step 4: Instantiate the App Context by calling “CreateAppEventClientContext” method shipped with TokenHelper.cs Class to facilitates App Authentication Tokens.

Step 5: Load the Products List using traditional CSOM way.

Step 6: Instantiate new Event Receiver Object using “EventReceiverDefinitionCreationInformation” class. It is important to set the following properties to get this Event Receiver added to the collection of Event Receivers of Product List.

Important Properties to set are,

  • EventType: Specifies the type of Event that can trigger receiver, like here we are assigning it to “Item Deleting” Even.
  • ReceiverName: Specifies the unique name of this Event Receiver that can be used to identify it in Event Receivers Collection of Products List.
  • ReceiverUrl: Specifies the full path to Receiver. Assign the value of the Full Path that we have retrieved in Step 2.
  • SequenceNumber: Specifies the Sequence Number for the Event Receiver to add to the Event Receivers Collection of the Product List.

Step 7: Add the Event Receiver Object to Products List Event Receivers Collection,

Add

Step 8: The other Event that we consider is “AppUninstalling”. This Event is quite useful to perform cleanup activities like removing the Event Receiver registration as soon as the App gets uninstalled.

Step 9: Same as Step 4 above.

Step 10: Same as Step 5 above.

Step 11: Loop through all the Event Receivers registered with Products List and find a specific Event Receiver by Name value (recall Step  6 where we have specified value to “ReceiverName” Property of Event Receiver).

Step 12: Once we identified the specific Event Receiver we can remove it from Event Receivers Collection of Products List.

Products

Step 13: This involves Handling of ItemDeleting Event of Products List. We can check the incoming Event Type by using “EventType” property of Event Object (properties).

Step 14: Validates the name of the list that generates the Event, this step is necessary to ensure that we are responding to the right Event generated from an intended list.

Step 15: Set “ErrorMessage” that needs to be displayed to the User in case User actions try to delete List Items.

Step 16: Set “Status” to “CancelWithError” since we want to cancel the User action along with showing him the proper error message.

CancelWithError

That’s it. We are all set to see the efforts in action.

Build and Deploy Solution to SharePoint.

Trust the App when asked to and move on.

Trust

Trust

Once deployed successfully we can test the Remote Event Receiver as follows,

Navigate to the Products List

Try to Delete any Item in the list and see if you are able to see the Error Message you set with in Event Receiver’s Properties.Properties


Properties

Sure enough you will get the Error Message displayed on the Error Page to which SharePoint navigates you to.

Error

This implementation is really amazing and I can see lot of potential in terms of handling discrete Business Rules and Validation on the existing SharePoint Lists and Libraries without deploying any Server Code in SharePoint Domain.