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.
Then choose a SharePoint developer site and then choose Provider-hosted like below.
Then it will ask to give credentials for the developer site and then it will automatically select the SharePoint version like below:
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.
Then choose “Use Windows Azure Access Control Service (for SharePoint cloud apps)” in the Configure authentication settings like below:
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.
Now select the Add-in project and from the properties make True to “Handle App Installed” like below:
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.
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.
Here choose Remote Event Receiver in the Add New Item dialog box.
Then choose List Item Events, event source as Custom List and then event as “An Item was added”.
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:
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:
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:
Then open the web.config file and replace the ClientId and ClientSecret which we have generated in the above page.
It looks like below:
Now open the AppManiest.xml file and give full control to the Site Collection like below:
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.
There add the ClientId in RemoteWebApplication inside AppPrincipal tag. AppManifest.xml looks like below:
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:
In the Publish Web project, Click on Microsoft Azure Web Apps like below:
Here login to the Azure account and it will display all the web apps. Choose the particular web apps like below:
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.
Then click on the Next as shown in the fig below:
You can also click on the Start Preview button to see the preview.
Then right-click on the Add-in Project and click on Publish like below:
Then in the Publish your app page click on the Edit button like below:
Then here put the ClientId and Client Secret which we have created in the above step. It looks like below:
Now click on the Deploy your web project again like below:
Once the web project deployed successfully, click on the Package the app like below:
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.
Once the .app file is ready, we will deploy the .app file to the Apps in a Testing list in the developer site.
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:
You may like the following SharePoint Online tutorial:
- SharePoint get current user id, name, email, display name programmatically
- Create a timer job in SharePoint 2016/2013 Programmatically (Step by Step tutorial)
- SharePoint jslink Examples
- Simple HTML form design examples with code
- Registration form design in HTML and CSS with code
- SharePoint Workflow history list URL
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.
After working for more than 15 years in Microsoft technologies like SharePoint, Office 365, and Power Platform (Power Apps, Power Automate, and Power BI), I thought will share my SharePoint expertise knowledge with the world. Our audiences are from the United States, Canada, the United Kingdom, Australia, New Zealand, etc. For my expertise knowledge and SharePoint tutorials, Microsoft has been awarded a Microsoft SharePoint MVP (9 times). I have also worked in companies like HP, TCS, KPIT, etc.