Steps to create a Remote event receiver using visual studio 2015 for SharePoint Online and deploy into Microsoft Azure

deploy remote event receiver sharepoint online

We can develop remote event receiver in SharePoint online using visual studio 2015. In SharePoint online Office 365 environments we can not run custom code in the SharePoint server. But we can run code in a remote server. Remote event receivers can be developed using provder hosted add-in. We can use remote event receivers to handle events in the SharePoint add-in model. We can use AppInstalled and AppUninstalling events to set up or remove SharePoint objects and other event receivers your add-in needs.

Here we will discuss how we can create a remote event receiver using provider hosted add-in using visual studio 2015 in SharePoint online site. We will deploy in Microsoft Azure.

Here we will discuss how we can create a provider hosted add-in, then how we can add a remote event receiver and then we will see how we can deploy it into 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.

Follow below steps to develop an event receive using visual studio 2015:

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.

remote event receiver
remote event receiver

Then choose a SharePoint developer site and then choose Provider-hosted like below. You can see below article to know how we can create developer site in SharePoint.

remote event receiver sharepoint online
remote event receiver sharepoint online

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

remote event receivers sharepoint 2013 provider hosted
remote event receivers sharepoint 2013 provider hosted

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

sharepoint 2013 remote event receiver example
sharepoint 2013 remote event receiver example

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





remote event receiver sharepoint 2013
remote event receiver sharepoint 2013

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

remote event receiver sharepoint 2016
remote event receiver sharepoint 2016

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

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

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.

remote event receivers debugging in sharepoint
remote event receivers debugging in sharepoint

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.

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

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

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

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

remote event receiver provider hosted app example
remote event receiver provider hosted app example

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:

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

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

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

create remote event receivers sharepoint 2013
create remote event receivers sharepoint 2013





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 web site which we have created in Azure (without https://). And the Redirect URI put the web site 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 web site in Microsoft Azure for SharePoint Online

Here the page looks like below:

debug remote event receivers sharepoint 2013
debug remote event receivers sharepoint 2013

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

It looks like below:

debug remote event receiver provider hosted app
debug remote event receiver provider hosted app

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

debug remote event receiver sharepoint 2013 on premise
debug remote event receiver sharepoint 2013 on premise

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.

debug remote event receiver sharepoint online
debug remote event receiver sharepoint online

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

sharepoint online remote event receiver tutorial
sharepoint online remote event receiver tutorial

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.

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;
}

RemoteEventReceiver1 code:

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

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) { }
}
}
}
}

Now we need to deploy the remote event receiver.





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

what is remote event receiver sharepoint 2013
what is remote event receiver sharepoint 2013

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

how to deploy remote event receiver sharepoint 2013
how to deploy remote event receiver sharepoint 2013

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 sharepoint online
deploy remote event receiver sharepoint online

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

sharepoint online remote event receiver host web
sharepoint online remote event receiver host web

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

sharepoint online remote event receiver not firing
sharepoint online remote event receiver not firing

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

sharepoint online remote event receiver azure
sharepoint online remote event receiver azure

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

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

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

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

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 2013
deploy remote event receiver to Microsoft Azure sharepoint 2013

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

sharepoint online the remote event receiver callout failed
sharepoint online the remote event receiver callout failed

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

Deploy remote event receiver steps
Deploy remote event receiver

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.

Create remote event receiver sharepoint
Create remote event receiver sharepoint

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

Create remove event receiver visual studio 2017
Create remove event receiver visual studio 2017

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:

How to create remote event receiver SharePoint visual studio 2017
How to create remote event receiver SharePoint visual studio 2017

Hope this will be helpful.

Similar SharePoint 2013 Tutorials


About Bijay Kumar

I am Bijay from Odisha, India. Currently working with a reputed organization in Bangalore. I am Microsoft Office Servers and Services (SharePoint) MVP (4 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.

View all posts by Bijay Kumar →