Remote Events in SharePoint 2013

 Aug 13, 2015

Earlier versions of SharePoint provided support for event receivers that allowed server side code to run in response to events like adding items to lists, creating new lists, activating a feature etc. With the new App model introduced in SharePoint 2013 we can't have an App that executes server side code in SharePoint and, consequently, the familiar concept of event receivers is not an option for your apps.

In today's blog I will address the new concept of Remote Event Receivers that play a role similar to event receivers but are implemented in a quite different way.

Event receivers are fully supported in SharePoint 2013 farm solutions and sandbox solutions in the same way as we had in earlier versions of SharePoint. The event is declared in an elements manifest file where we specify the type of event and where is the assembly with the code to handle the event. We write the event handler by providing a class that inherits from the correct event receiver base class and we deploy the assembly in the global assembly cache. In Visual Studio 2012 or 2013 a Wizard will take care of all the details and you end up just having to start writing the code for the event handler as you see in the following code snippet:

public class MyEventReceiver : SPItemEventReceiver { public override void ItemAdding ( SPItemEventProperties properties) { . . . } }

Besides ItemAdding which is triggered before an item is added to a list we have several other events, as for example ItemAdded which is triggered after the item is added, ItemDeleting, ItemDeleted, ItemUpdating, ItemUpdated, ItemCheckingIn, etc.

Note that the base class we are using is SPItemEventReceiver which is the correct option if we want to implement event handlers for actions happening to items in a list. There are other options like SPListEventReceiver and SPWebEventReceiver.

Remote Event Receivers were introduced in SharePoint 2013 and the main difference is that the code that runs when the event is triggered is hosted by a WCF service and not by an assembly that is added to the GAC of the server where SharePoint is running. This WCF service runs outside of the SharePoint process and it could be hosted in the cloud or in an on-premise server that is not being used as a SharePoint server.

Now let's walk through the steps to create a remote event receiver for a SharePoint app.

1. Open or create an App for SharePoint Project using Visual Studio 2012 or 2013 and then right-click the project node in Solution Explorer, select Add New Item and pick the template for Remote Event Receiver.

2. You can specify what type of remote event you want and the wizard takes care of creating a feature with a suitable elements manifest file and some boilerplate code where you will start adding the useful functionality to respond to the event. Let's say you choose List Item Events for an Announcements list. Make sure you add one instance of the Announcements list to the project.

3. You will see that the wizard adds a svc file with boilerplate code to implement the remote event. The code is quite different from the one we have in the previous code snipet for the non-remote event receiver as you can see below:

public class RemoteEventReceiver1 : IRemoteEventService { public SPRemoteEventResult ProcessEvent ( SPRemoteEventProperties properties ) { . . . } public void ProcessOneWayEvent ( SPRemoteEventProperties ) { . . . } }

For remote events we need a class that implements the interface IRemoteEventService. This interface has only 2 methods: ProcessEvent and ProcessOneWayEvent.

ProcessEvent handles events that occur before an action occurs and ProcessOneWayEvent handles events that occur after the action. The first runs synchronously and returns a response, the second runs asynchronously and returns nothing.

The code you can provide in these methods will have access to the Client Side Object Model (CSOM) only, so it is quite distinctive from the code you can write for non-remote events receivers which gives you access to server objects like SPList, SPWeb, SPSite etc.

To learn more about SharePoint 2013 development we recommend the course 20488 : Developing Microsoft SharePoint Server 2013 Core Solutions.

How do your Excel skills stack up?   

Test Now  

About the Author:

Newton Godoy  

With over 17 years of in-class training experience and over 16 years of industry experience, Newton offers students a wealth of real-world technical knowledge and expertise in the areas of .NET application development, SQL Server and SharePoint Server. After spending several years lecturing as a professor, Newton found his true calling and began his career as a MCT. He worked as a technical trainer for some of Brazil’s and Australia’s largest corporate training organisations before finally finding a home with New Horizons where he is now one of our top trainers. Newton brings a thorough mentoring capability to the classroom where he can advise on technical issues and challenges often beyond the scope of the course curriculum. His combination of technical knowledge and instructor experience make him one of the most respected instructors within the IT training industry.

Read full bio
top