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
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.
Next , 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
-------------------------------------------------
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();
}
}
}
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 kindly contact me.
Happy Coding !!!
Thanks,
Pankaj Srivastava