Working with Remote Event Receivers in a Provider Hosted Add-in

This article shows how you extend a SharePoint provider hosted Add-in by adding a remote event receiver (RER) that handles the ItemAdded event for a custom list in the add-in web. you should have basic understanding of provider-hosted SharePoint Add-ins before starting work on RER.

So Let’s begin ..

1. Right click on your HostWebProject add new Remote Event receiver – I am using here Item added event

2. Now as you can see your event receiver will be added to solution so once you will add event receiver one RemoteEventReciver1.svc file will be created in AppWeb Solution.

3. Once your RemoteEventReceiver has been added to solution, open your element.xml file update your  .svc Location URL

 

4.  .svc file will be created in your AppWebProject

Now Write your code on RemoteEventReciver1.svc.cs

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 your event receiver will be created and when you run “deploy” this will not work! I was facing exactly the same problem with my Event Receiver not triggering,  so the question is – how do we overcome from this issue? Okay, so lets move ahead.

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

Open AppEventReceiver.svc.cs file add below code

Now I have written my code on AppInstalled Event-  which will attached RemoteEventReceiver1 to My List

 

————————————————-

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

 

After following all the above steps deploy your code..

Cheers !!

Add a Comment

Your email address will not be published. Required fields are marked *