Create remote event receiver SharePoint Online Step by Step Tutorial

This SharePoint Online tutorial explains, how we can develop remote event receiver (RER) in SharePoint online using visual studio 2015/2017 as a provider-hosted add-in and how we can deploy remote event receiver to Microsoft Azure

Previously in SharePoint 2013 or any on-premise version, we can write farm solution code to develop event receivers.

But in SharePoint Online Office 365 environments we cannot run custom code in the SharePoint server. Her we can run code in a remote server. Remote event receivers can be developed using a provider hosted add-in.

Here we will see, how we can create a remote event receiver using a provider hosted add-in using visual studio 2015 in SharePoint Online site. And then we discuss how we can deploy remote event receiver to Microsoft Azure.

We will attach the remote event receiver to a list which will be in the host web in the SharePoint Online site and will trigger on ItemAdded event. We will use the AppInstalled event to set up the event.

The remote event receiver can be hosted in the cloud or in an on-premise server that is not also being used as a SharePoint server but here we will see how we can host remote event receiver (RER) into Microsoft Azure.

Create remote event receiver SharePoint Online step by step

Below are the steps to create remote event receiver in SharePoint Online step by step tutorial.

Open visual studio 2015 and then File -> New Project. Then in the New Project dialog box, choose Templates -> Visual C# -> Office/SharePoint and from there choose App for SharePoint like below. Give a name and make sure.Net Framework version should be chosen 4.5.

create remote event receiver sharepoint online
create remote event receiver sharepoint online

Then choose a SharePoint developer site and then choose Provider-hosted like below.

create remote event receiver sharepoint online step by step
create remote event receiver sharepoint online step by step

Then it will ask to give credentials for the developer site and then it will automatically select the SharePoint version like below:

Steps to create a Remote event receiver using visual studio 2015
Steps to create a Remote event receiver using visual studio 2015

Then in the web project type, select whether you want to create an ASP.NET Web Forms Application or ASP.NET MVC Web Application like below. Here we will choose ASP.NET Web Forms Application.

Create a remote event receiver in SharePoint Add-ins
Create a remote event receiver in SharePoint Add-ins

Then choose “Use Windows Azure Access Control Service (for SharePoint cloud apps)” in the Configure authentication settings like below:

How do I create a remote event receiver in SharePoint online?
How do I create a remote event receiver in SharePoint online?

Visual studio will take some time and will create two projects one (Add-in and another one is the ASP.NET Web project) like below.

rer in sharepoint online
rer in sharepoint online

Now select the Add-in project and from the properties make True to “Handle App Installed” like below:

remote event receiver in sharepoint online
remote event receiver in sharepoint online

Once you make the property true. It is going to add and AppEventReceiver.svc inside the Services folder like below: In this class, we are going to handle our AppInstalled and AppUninstalling event.

create a SharePoint online event receiver
create a SharePoint online event receiver

Then we will add our remote event receiver class. For this Right click on the Add-in project (first project) and then Add -> New Item.

create remote event receiver sharepoint online step by step using visual studio
create remote event receiver sharepoint online step by step using visual studio

Here choose Remote Event Receiver in the Add New Item dialog box.

Steps to create a Remote event receiver using visual studio 2017
Steps to create a Remote event receiver using visual studio 2017

Then choose List Item Events, event source as Custom List and then event as “An Item was added”.

remote event receivers in sharepoint 2013
remote event receivers in sharepoint 2013

Once you add the remote event receiver, the .svc (RemoteEventReceiver1.svc) file will be added into the App project as well as inside the Services folder inside the Web project like below:

remote event receivers in sharepoint 2013 online
remote event receivers in sharepoint 2013 online

Here is my host web I have added a SharePoint list which a Title and Description column.

When a user adds an item to the list, the user will add Title and the Description will be filled from the remote event receiver. The list looks like below:

remote event receivers in sharepoint online
remote event receivers in sharepoint online

Now we need to generate the ClientId and ClientSecret. So navigate to the _layouts/15/appregnew.aspx page

Example: https://onlysharepoint2013.sharepoint.com/sites/MyO365DevSite/_layouts/15/appregnew.aspx

There click on the Generate button for the Client Id and Client Secret fields. Then give a title and Domain put the website which we have created in Azure (without https://). And the Redirect URI put the website URL, make sure to give in https:

If you are new to Azure web app creation then you can check the below article:

  • Steps to create a website in Microsoft Azure for SharePoint Online

Here the page looks like below:

remote event receivers in sharepoint 2016
remote event receivers in sharepoint 2016

Then open the web.config file and replace the ClientId and ClientSecret which we have generated in the above page.

It looks like below:

create remote event receivers sharepoint online
create remote event receivers sharepoint online

Now open the AppManiest.xml file and give full control to the Site Collection like below:

sharepoint online remote event receiver example
sharepoint online remote event receiver example

Also, we need to add the ClientId in AppManifest.xml file. Right click on the AppManifest.xml file and Click on View code. In the warning, box click on Yes which will open the code file.

create remote event receiver sharepoint online
create remote event receiver sharepoint online

There add the ClientId in RemoteWebApplication inside AppPrincipal tag. AppManifest.xml looks like below:

create remote event receiver sharepoint online step by step
create remote event receiver sharepoint online step by step

AppEventReceiver Code:
In the AppEventReceiver file, we need to add the code to attach the event receiver to the host list. The code will be added in the ProcessEvent event. Here we are adding the AppInstalled and AppUninstalling event.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.EventReceivers;
using System.Reflection;

namespace DemoSharePointRERWeb.Services
{
public class AppEventReceiver : IRemoteEventService
{
/// <summary>
/// Handles app events that occur after the app is installed or upgraded, or when app is being uninstalled.
/// </summary>
/// <param name=”properties”>Holds information about the app event.</param>
/// <returns>Holds information returned from the app event.</returns>

public SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties)
{
SPRemoteEventResult result = new SPRemoteEventResult();
if (properties.EventType == SPRemoteEventType.AppInstalled)
{
using (ClientContext clientContext = TokenHelper.CreateAppEventClientContext(properties, false))
{
if (clientContext != null)
{
//Get reference to the host web list with name Feedback
var documentsList = clientContext.Web.Lists.GetByTitle(“DemoRemoteEventReceiverList”);
clientContext.Load(documentsList);
clientContext.ExecuteQuery();
string remoteUrl = “https://enjoysharepoint.azurewebsites.net/Services/RemoteEventReceiver1.svc”;
//Create the remote event receiver definition
EventReceiverDefinitionCreationInformation newEventReceiver = new EventReceiverDefinitionCreationInformation()
{
EventType = EventReceiverType.ItemAdded,
ReceiverAssembly = Assembly.GetExecutingAssembly().FullName,
ReceiverName = “RemoteEventReceiver1”,
ReceiverClass = “RemoteEventReceiver1”,
ReceiverUrl = remoteUrl,
SequenceNumber = 15001
};
//Add the remote event receiver to the host web list
documentsList.EventReceivers.Add(newEventReceiver);
clientContext.ExecuteQuery();
}
}
}
else if (properties.EventType == SPRemoteEventType.AppUninstalling)
{
using (ClientContext clientContext = TokenHelper.CreateAppEventClientContext(properties, false))
{
var list = clientContext.Web.Lists.GetByTitle(“DemoRemoteEventReceiverList”);
clientContext.Load(list);
clientContext.ExecuteQuery();
EventReceiverDefinitionCollection erdc = list.EventReceivers;
clientContext.Load(erdc);
clientContext.ExecuteQuery();
List<EventReceiverDefinition> toDelete = new List<EventReceiverDefinition>();
foreach (EventReceiverDefinition erd in erdc)
{
if (erd.ReceiverName == “RemoteEventReceiver1″)
{
toDelete.Add(erd);
}
}

//Delete the remote event receiver from the list, when the app gets uninstalled

foreach (EventReceiverDefinition item in toDelete)
{
item.DeleteObject();
clientContext.ExecuteQuery();
}
}
}
return result;
}

/// <summary>
/// This method is a required placeholder, but is not used by app events.
/// </summary>
/// <param name=”properties”>Unused.</param>

public void ProcessOneWayEvent(SPRemoteEventProperties properties)
{
throw new NotImplementedException();
}
}
}

RemoteEventReceiver1 code:
In the remote event receiver code, we will write the code which will handle the ItemAdded event.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.EventReceivers;
namespace DemoSharePointRERWeb.Services
{
public class RemoteEventReceiver1 : IRemoteEventService
{
/// <summary>
/// Handles events that occur before an action occurs, such as when a user adds or deletes a list item.
/// </summary>
/// <param name=”properties”>Holds information about the remote event.</param>
/// <returns>Holds information returned from the remote event.</returns>

public SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties)
{
SPRemoteEventResult result = new SPRemoteEventResult();
using (ClientContext clientContext = TokenHelper.CreateRemoteEventReceiverClientContext(properties))
{
if (clientContext != null)
{
clientContext.Load(clientContext.Web);
clientContext.ExecuteQuery();
}
}
return result;
}

/// <summary>
/// Handles events that occur after an action occurs, such as after a user adds an item to a list or deletes an item from a list.
/// </summary>
/// <param name=”properties”>Holds information about the remote event.</param>

public void ProcessOneWayEvent(SPRemoteEventProperties properties)
{

// On Item Added event, the list item creation executes

if (properties.EventType == SPRemoteEventType.ItemAdded)
{
using (ClientContext clientContext = TokenHelper.CreateRemoteEventReceiverClientContext(properties))
{
if (clientContext != null)
{
try
{
string title = properties.ItemEventProperties.AfterProperties[“Title”].ToString();
List lstDemoeventReceiver = clientContext.Web.Lists.GetByTitle(properties.ItemEventProperties.ListTitle);

ListItem itemDemoventReceiver = lstDemoeventReceiver.GetItemById(properties.ItemEventProperties.ListItemId);
itemDemoventReceiver[“Description”] = “Description from RER :: “+title;
itemDemoventReceiver.Update();
clientContext.ExecuteQuery();
}
catch (Exception ex) { }
}
}
}
}
}
}

Deploy Remote Event Receiver in SharePoint Online to Microsoft Azure

Now we need to deploy the remote event receiver.

Right click on the web project and then click on Publish like below:

Steps to create a Remote event receiver using visual studio 2015
Steps to create a Remote event receiver using visual studio 2015

In the Publish Web project, Click on Microsoft Azure Web Apps like below:

deploy create remote event receiver sharepoint online
deploy create remote event receiver sharepoint online

Here login to the Azure account and it will display all the web apps. Choose the particular web apps like below:

deploy remote event receiver to Microsoft Azure sharepoint online
deploy remote event receiver to Microsoft Azure sharepoint online

Then it will display the Publish Web screen with Publish method, Server, Site name, Username, Password and Destination URL. Do the validate connection and once it is successful. Then click on Next.

deploy remote event receiver to Microsoft Azure sharepoint online 2013
deploy remote event receiver to Microsoft Azure sharepoint online 2013

Then click on the Next as shown in the fig below:

create and deploy remote event receiver sharepoint online
create and deploy remote event receiver sharepoint online

You can also click on the Start Preview button to see the preview.

create and deploy remote event receiver sharepoint online
create and deploy remote event receiver sharepoint online

Then right-click on the Add-in Project and click on Publish like below:

create remote event receiver sharepoint online step by step
create remote event receiver sharepoint online step by step

Then in the Publish your app page click on the Edit button like below:

deploy remote event receiver to Azure sharepoint online
deploy remote event receiver to Azure sharepoint online

Then here put the ClientId and Client Secret which we have created in the above step. It looks like below:

deploy remote event receiver to Microsoft Azure sharepoint online
deploy remote event receiver to Microsoft Azure sharepoint online

Now click on the Deploy your web project again like below:

sharepoint online remote event receiver deployment
sharepoint online remote event receiver deployment

Once the web project deployed successfully, click on the Package the app like below:

create remote event receivers sharepoint online
create remote event receivers sharepoint online

Here the website will be populated by default and the clientId also will be populated by default like below. Click on Finish button which will generate the .app file.

remote event receiver sharepoint online step by step
remote event receiver sharepoint online step by step

Once the .app file is ready, we will deploy the .app file to the Apps in a Testing list in the developer site.

remote event receiver sharepoint 2013 step by step
remote event receiver sharepoint 2013 step by step

Once the app installed successfully, now open the list and add an item to the list. We will only add the Title and the description will be populated by the remote event receiver like as shown in the figure below:

sharepoint online remote event receiver example
sharepoint online remote event receiver example

You may like the following SharePoint Online tutorial:

Hope this SharePoint tutorial helps how to create remote event receiver SharePoint online step by step. We also saw how we can deploy the remote event receiver to Microsoft Azure.

>