Remote Event Receivers in SharePoint 2013

Remote Event receiver is one of the two way notification options provided by the new Remote Event Infrastructure in SharePoint 2013.

In SharePoint 2013, remote event receivers are built as web services and can be one-way or two-way.

The remote event receivers can be registered with SharePoint 2013 just like the way local event receivers are registered in SharePoint 2010. The only difference is now we need to provide a web service URL in place of an assembly and class name.

Whenever an event is raised by a SP component ( like a list or site), SharePoint calls the web service and sends the event properties to the web service which carries out its business. The web service can also call back into SharePoint, authenticated via OAuth, to read and write as needed.

Below is a sample  Elements.xml of a remote event receiver registered for a list deployed as part of a SharePoint-hosted App. The web service URL and event type is specified inside in the <Url> and <Type> tags

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
   <Receivers ListUrl="/lists/contactus">
      <Receiver>
        <Name>ContactUsListItemAdded</Name>
          <Type>ItemAdded</Type>
          <SequenceNumber>10000</SequenceNumber>
          <Url>http://techiesmeet.in/ContactUsReceiver.svc</Url>
      </Receiver>
    </Receivers>
</Elements>

To create a web service for the remote event receiver, we need to implement the IRemoteEventService interface. There are two methods to be implemented from the interface:

  • ProcessEvent  This is a synchronous event handler.Use it to call code when a “before” event occurs .
  • ProcessOneWayEvent This is an asynchronous event handler. Use it to call code when an “after” event occurs .

Both the methods accept  RemoteEventProperties object as parameter from which you can determine everything about the occurred event.

The important point about the ProcessEvent is it is for synchronous processing, so SharePoint will wait until your code in the remote webservice completes. To allow cancel the event, the ProcessEvent returns an SPRemoteEventResult object that you can optionally use to let SharePoint  know how to proceed after remote webservice code is executed. The ProcessOneWayEvent method is for asynchronous processing and returns void.

For synchronous “-ing” events (like  adding, deleting, updating)  you must use the ProcessEvent method. For asynchronous  ”-ed”  events (like  added, updated, deleted), use the ProcessOneWayEvent method.

You can also use ProcessEvent for the  ”-ed” events , if you want synchronous processing . However, “-ed” events cannot be canceled by your remote web-service code , because the event has already occurred in SharePoint.

The below diagram demonstrates the  flow of how a remote event receiver actually works.

 

Below is an example of a web-service class used as remote event receiver:

public class ContactUsReceiver : IRemoteEventService
{
     public SPRemoteEventResult ProcessEvent(RemoteEventProperties properties)
     { 
       switch (properties.EventType)
           {
             case RemoteEventType.ItemAdding:
             //Code to handle ItemAdding
             break; 
             case RemoteEventType.ItemDeleting:
             //Code to handle ItemDeleting
             break;
           }
         return new SPRemoteEventResult();
     }

     public void ProcessOneWayEvent(RemoteEventProperties properties)
     {
        if (properties.EventType == RemoteEventType.ItemAdded)
         {
              //Code to handle ItemAdded
         }
       if (properties.EventType == RemoteEventType.ItemDeleted)
        {
             //Code to handle ItemDeleted
        }
    }
}

Below are some excellent resources to have good grasp on remote event receivers:

SharePoint 2013: Use event receivers to handle events in apps for SharePoint

Remote Event Receivers – Demo

 

2 Responses to “Remote Event Receivers in SharePoint 2013”
  1. [...] Remote event receivers(for Apps) [...]

  2. Suresh Pydi says:

    Nice post. Here is one more post Explaining Sharepoint 2013 remote Event handlers

  3. Dylan Nicholson says:

    Can you confirm then that the remote event receiver web service handling the event must then allow anonymous access, as when SharePoint calls the service it has no credentials it can use to authenticate itself?

Leave a Reply

Subscribe

Get every post delivered to your inbox via FeedBurner :

© 2010-2013 Extreme Sharepoint | The content is copyrighted to Amit Kumawat and may not be reproduced on other websites.