Hi All, I would like to share my experiences while working on provider-hosted App Remote Event Receiver. When I was working with Remote Event Receiver’s for the first time I faced a lot of challenges. For example, I was not able to find why my Remote Event Receiver was not triggering and after a lot of investigation, I found the cause of the problem. If you are new to provider-hosted App Remote Event Receiver’s this article will help you a lot.
1. Right click on your HostWebProject add new Remote Event receiver – I am using here Item added event




I have written my code on ProcessOnWayEvent that fires after an action occurs, such as after a user adds an item to a list (because I have chosen on ItemAdded)

Now, right-click on your HostWeb Project – double click on “Handle App Installed” (if you want to Perform something when your App Installed).
When “Handle App Installed” is “true” then it will generate a new AppEventReceiver.svc in the AppWebProject


private void HandleAppInstalled(SPRemoteEventProperties properties)
{
try
{
using (ClientContext clientContext = TokenHelper.CreateAppEventClientContext(properties, false))
{
if (clientContext != null)
{
List olist = clientContext.Web.Lists.GetByTitle("TestCustom");
clientContext.Load(olist, o => o.EventReceivers);
clientContext.ExecuteQuery();
bool isReRExsist = false;
foreach (var receiver in olist.EventReceivers)
{
if (receiver.ReceiverName == "RemoteEventReceiver1")
{
isReRExsist = true;
break;
}
}
if (!isReRExsist)
{
string remoteUrl = "https://xyz.azurewebsites.net/Services/RemoteEventReceiver1.svc";
EventReceiverDefinitionCreationInformation eventReDefCreation = new EventReceiverDefinitionCreationInformation()
{
EventType = EventReceiverType.ItemAdded,
ReceiverAssembly = Assembly.GetExecutingAssembly().FullName,
ReceiverName = "RemoteEventReceiver1",
ReceiverClass = "RemoteEventReceiver1",
ReceiverUrl = remoteUrl,
SequenceNumber = 15000
};
olist.EventReceivers.Add(eventReDefCreation);
clientContext.ExecuteQuery();
}
}
}
}
catch (Exception ex)
{
}
}
and detaching my EventReceiver from List when my app will uninstalled from site
private void HandleAppUnistalled(SPRemoteEventProperties properties)
{
using (ClientContext clientContext = TokenHelper.CreateAppEventClientContext(properties, false))
{
var list = clientContext.Web.Lists.GetByTitle("TestCustom");
clientContext.Load(list);
clientContext.ExecuteQuery();
EventReceiverDefinitionCollection eventRColl = list.EventReceivers;
clientContext.Load(eventRColl);
clientContext.ExecuteQuery();
List<EventReceiverDefinition> toDelete = new List<EventReceiverDefinition>();
foreach (EventReceiverDefinition erdef in eventRColl)
{
if (erdef.ReceiverName == "RemoteEventReceiver1")
{
toDelete.Add(erdef);
}
}
//Delete the remote event receiver from the list, when the app gets uninstalled
foreach (EventReceiverDefinition item in toDelete)
{
item.DeleteObject();
clientContext.ExecuteQuery();
}
}
}
Now deploy your code it will work fine….
I hope my article will help you guys pls. drop your comment if you will face any issue.
Happy Coding !!!
Thanks,
Pankaj Srivastava
