Tuesday, 13 November 2012

Step-by-step: creating Remote Event Receiver

In this example I will show, how to create autohosted SharePoint 2013 app with Remote Event Receiver, and then how to deploy it to Office 365 Developer Preview.

Start with creating a list in SharePoint site, to which you will attach Event Receiver.
Go to site contents and create new Announcements list.
In Visual Studio, create new project type: App for Office 2013. For hosting type, choose autohosted.
In Solution Explorer, right-click on newly created project and choose Add > New Item. Add Remote Event Receiver


In next dialog, choose list type and which events will be handled.

You will notice, that new project has been added to your solution. This is a web service, that will be hosted outside SharePoint, on Windows Azure. SharePoint will make calls to this web service, that contain code to handle remote events.
At this step, Visual Studio has generated some boilerplate code, that you will need to extend.
Notice 'TokenHelper.cs' file. This file contains ready to use methods, to retrieve SharePointContextToken. This context token can be later used to call back to SharePoint. You can make calls to SharePoint using Client-Side Object Model (CSOM).

If you selected several different events for your list, like eg. ItemAdded, ItemUpdated, ItemDeleted, etc., they will all be handled by ProcessEvent method in web service. To be able to differentiate between various event types, use RemoteEventProperties.EventType property.

    switch (properties.EventType)
    {
        case RemoteEventType.ItemAdding:
            break;
        case RemoteEventType.ItemUpdating:
            break;
        case RemoteEventType.ItemDeleting:
            break;
        default:
            break;
   }
When you finish writing Event Receiver code, deploy it to website.
You should have already Site URL in project properties set. Select Build > Deploy Solution. Visual Studio will connect to your website, ask you for login and password and then deploy solution. You will have to confirm, that you trust this app.

More reading on MSDN:
[3] Bing Video - Remote event receivers in SharePoint 2013 demo (video tutorial for preview version of SharePoint15)

No comments:

Post a Comment