SharePoint 2013 - Add Remote Event Receiver Programmatically
Below is sample code of associate a remove event receiver to a list in the host web. This is assuming that you have already add the remote event receiver in the project.
public SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties)
{
SPRemoteEventResult result = new SPRemoteEventResult();
if (properties.EventType == SPRemoteEventType.AppInstalled)
{
using (ClientContext clientContext = TokenHelper.CreateAppEventClientContext(properties, false))
{
if (clientContext != null)
{
EventReceiverDefinitionCreationInformation eventReceiver = new EventReceiverDefinitionCreationInformation();
eventReceiver.EventType = EventReceiverType.ListAdded;
eventReceiver.ReceiverAssembly = Assembly.GetExecutingAssembly().FullName;
eventReceiver.ReceiverClass = "RCEventReceiverWeb.Services.RCListRemoteEventReceiver";
eventReceiver.ReceiverName = "RCListRemoteEventReceiverListAdded";
eventReceiver.ReceiverUrl = "https://rchen.com/rceventreceiver/Services/RCListRemoteEventReceiver.svc";
eventReceiver.SequenceNumber = 1000;
Web web = clientContext.Web;
web.EventReceivers.Add(eventReceiver);
clientContext.ExecuteQuery();
}
}
}
return result;
}
Comments