none
How to Add Remote Event Receiver on SharePoint 2013 Online Custom List? RRS feed

  • Question

  • Hi,

    I have two existing custom lists (say List A and List B) on my SharePoint 2013 Online site. I want to add a remote event receiver to List A so that whenever items is added, updated or deleted in/from List A, the manipulation should take place in List B according to some logic.

    Is it possible to add remote event receivers to existing custom lists for SharePoint 2013 Online site? If yes, how?

    I tried searching over the Internet but I could not find any helpful information on how to create remote event receiver for existing custom lists. 

    Thanks in advance!

    Tuesday, September 20, 2016 5:14 AM

Answers

  • Hi Sunny,

    Launch the properties window of the App, and enable the App Installed, App Uninstalling and App Upgraded events as shown in the image below:

    This will automatically create an AppEventReceiver.svc file in the Services Folder of the Web Project.

    You can refer to the sample code inside the AppEventReceiver.svc.cs file shared by me aboveto attach the event receiver to the List A.

    Note: The endpoint url of the event receiver service file is different at the time of debugging using the Azure Service Bus and is different when the code is deployed in IIS.

    Assuming the name of the your Event Receiver Service file is RemoteEventReceiverTest.svc, below is the code snippet to dynamically get the Event Receiver Endpoint url:

     string remoteEventEndPointUrl = string.Empty;
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    // debugging url
                    string opContext = OperationContext.Current.Channel.LocalAddress.Uri.AbsoluteUri.Substring(
                      0, OperationContext.Current.Channel.LocalAddress.Uri.AbsoluteUri.LastIndexOf(Path.AltDirectorySeparatorChar));
    
                    remoteEventEndPointUrl = string.Format("{0}/RemoteEventReceiverTest.svc", opContext);
                }
                else
                {
                    HttpRequestMessageProperty requestProperty = (HttpRequestMessageProperty)OperationContext.Current.IncomingMessageProperties[HttpRequestMessageProperty.Name];
                    remoteEventEndPointUrl = string.Format("https://{0}/Services/RemoteEventReceiverTest.svc", requestProperty.Headers[HttpRequestHeader.Host]);               
                }


    Regards, Arcee Gomes

    Tuesday, September 20, 2016 10:37 AM
  • Hi Sunny,

    There are two ways to deploy your event receiver,

    1. You can deploy your remote event receiver by writing above code as Arcee Gomes mentioned above.

    2. When you publish your app you will get .WSP in app project BIN folder. You can directly upload that WSP file in solutions gallery Site Settings -> Web Designer Galleries -> Solutions and activate feature in desired sub site (Site Settings -> Site Actions -> Manage Site Features) . Remote event receiver gets attached with List Definition.

    Tuesday, September 20, 2016 12:02 PM

All replies

  • HI Sunny

    Check below URL

    http://sundarnarasiman.net/2014/03/28/how-to-create-a-remote-event-receiver-for-an-app-in-office-365-sharepoint-2013/


    Mark it as an answer if it helped you out. Regards Rahul Dagar

    Tuesday, September 20, 2016 6:05 AM
  • Hi Rahul,

    Thanks for your reply.

    I have already checked this. Please see my question again, I am asking about the remote event receivers for the existing lists in SharePoint 2013 Online site.

    In the link you have shared, user is adding a custom list to the solution.

    Tuesday, September 20, 2016 7:20 AM
  • Hi Rahul,

    Follow the same steps which is provided in above link (http://sundarnarasiman.net/2014/03/28/how-to-create-a-remote-event-receiver-for-an-app-in-office-365-sharepoint-2013/) and do not create new list definition instead select custom list template while create remote event receiver like below

    Use below code to trigger event to required list, ( In your case List A and B)

     if (properties.EventType == SPRemoteEventType.ItemAdded)
                    {
                        if (properties.ItemEventProperties.ListTitle.ToString().ToLowerInvariant().Equals("testlist"))
                        {                       
                        }                
                        
                    }

    If this answer helps you, please mark it as answered.

    Tuesday, September 20, 2016 7:38 AM

  • Tuesday, September 20, 2016 8:05 AM
  • Hi Sunny,

    I would like to suggest you to try to debug the remote event receiver to find more detailed information, you can refer the article below:

    Debugging Remote Event Receivers with Visual Studio

    Also, here is a detailed demo about creating remote event receiver for your reference:

    Create a remote event receiver in SharePoint Add-ins

    Please reach out to us in case of any further help required.


    Hope this help.

    Vote - if you find this helpful

    Marked as Answer - if you see this reply being an answer to the question of the thread


    Regards,

    Amjad Khan

    Blog: https://amjadk.com

    Tuesday, September 20, 2016 8:14 AM
  • Hi Sunny,

    You can programmatically attach Remote Event Receivers to an existing custom list using CSOM, by handling the AppInstalled and AppUninstalling events in the AppEventReceiver.svc.cs file. On AppInstalled event you need to attach the event receiver to the list and on AppUninstalling event you need to un-register the event receiver from the list.

    Below is the same code snippet on how to achieve this:

    public class AppEventReceiver : IRemoteEventService
        {
            /// <summary>
            /// Handles app events that occur after the app is installed or upgraded, or when app is being uninstalled.
            /// </summary>
            /// <param name="properties">Holds information about the app event.</param>
            /// <returns>Holds information returned from the app event.</returns>
            public SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties)
            {
                SPRemoteEventResult result = new SPRemoteEventResult();
    
                using (ClientContext clientContext = TokenHelper.CreateAppEventClientContext(properties, false))
                {
                    if (clientContext != null)
                    {
                        Web web = clientContext.Web;                    
                        List listA = web.Lists.GetByTitle("ListA");
                        clientContext.Load(web);
                        clientContext.Load(listA);
    
                        switch (properties.EventType)
                        {
                            case SPRemoteEventType.AppInstalled:
    
                                EventReceiverDefinitionCreationInformation itemAddedEventReceiver = new EventReceiverDefinitionCreationInformation
                                {
                                    EventType = EventReceiverType.ItemAdded,
                                    ReceiverName = "NameOfEventReceiver",
                                    ReceiverUrl = "endpoint url of the event receiver service file",
                                    SequenceNumber = 10000
                                };
    
                                listA.EventReceivers.Add(itemAddedEventReceiver);
                                clientContext.ExecuteQuery();
    
                                break;
    
    
                            case SPRemoteEventType.AppUninstalling:
    
                                EventReceiverDefinitionCollection eventColl = listA.EventReceivers;
                                clientContext.Load(eventColl);
                                clientContext.ExecuteQuery();
    
                                List<EventReceiverDefinition> toDelete = new List<EventReceiverDefinition>();
                                foreach (EventReceiverDefinition ev in eventColl)
                                {
                                    if (ev.ReceiverName == "NameOfEventReceiver")
                                    {
                                        toDelete.Add(ev);
                                    }
                                }
    
                                foreach (EventReceiverDefinition evr in toDelete)
                                {
                                    evr.DeleteObject();
                                }
    
                                clientContext.ExecuteQuery();
    
                                break;
    
                            default:
                                break;
                        }
                    }
                }
    
                return result;
            }
    
            /// <summary>
            /// This method is a required placeholder, but is not used by app events.
            /// </summary>
            /// <param name="properties">Unused.</param>
            public void ProcessOneWayEvent(SPRemoteEventProperties properties)
            {
                throw new NotImplementedException();
            }
    
        }

       


    Regards, Arcee Gomes

    Tuesday, September 20, 2016 9:14 AM
  • Thanks all for you replies.

    I have created a project and added a code to the "Remote Event Receiver" svc.cs file (sample is as shown below but the original code looks different with the logics used in it).

    How should I deploy this remote event receiver to my live SharePoint 2013 Online site on the specific "List A"?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.SharePoint.Client;
    using Microsoft.SharePoint.Client.EventReceivers;

    namespace EventReceiverSharePointAppTestProjWeb.Services
    {
        public class RemoteEventReceiverTest : IRemoteEventService
        {
            class ListData
            {
                public string StudyNum
                { get; set; }
                public string Campaign
                { get; set; }
                public string Media
                { get; set; }          
            }

            /// <summary>
            /// Handles events that occur before an action occurs, such as when a user adds or deletes a list item.
            /// </summary>
            /// <param name="properties">Holds information about the remote event.</param>
            /// <returns>Holds information returned from the remote event.</returns>
            public SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties)
            {
                SPRemoteEventResult result = new SPRemoteEventResult();

                using (ClientContext clientContext = TokenHelper.CreateRemoteEventReceiverClientContext(properties))
                {
                    if (clientContext != null)
                    {
                        clientContext.Load(clientContext.Web);
                        clientContext.ExecuteQuery();
                    }
                }

                return result;
            }

            /// <summary>
            /// Handles events that occur after an action occurs, such as after a user adds an item to a list or deletes an item from a list.
            /// </summary>
            /// <param name="properties">Holds information about the remote event.</param>
            public void ProcessOneWayEvent(SPRemoteEventProperties properties)
            {            
                if (properties.ItemEventProperties.ListTitle.Equals("List A") && properties.EventType == SPRemoteEventType.ItemAdded)
                {
                    ListData listData = new ListData();

                    if (properties.ItemEventProperties.AfterProperties["Study #"] != null)
                    {
                        listData.StudyNum = properties.ItemEventProperties.AfterProperties["Study #"].ToString();
                    }

                    if (properties.ItemEventProperties.AfterProperties["Campaign"] != null)
                    {
                        listData.Campaign = properties.ItemEventProperties.AfterProperties["Campaign"].ToString();
                    }

                    if (properties.ItemEventProperties.AfterProperties["Media"] != null)
                    {
                        listData.Media = properties.ItemEventProperties.AfterProperties["Media"].ToString();
                    }
                  
                    using (ClientContext clientContext = TokenHelper.CreateRemoteEventReceiverClientContext(properties))
                    {
                        if (clientContext != null)
                        {
                            clientContext.Load(clientContext.Web);
                            clientContext.ExecuteQuery();
                            List listB = clientContext.Web.Lists.GetByTitle("List B");
                            clientContext.Load(listB);
                            clientContext.ExecuteQuery();

                            if (listB != null)
                            {                           
                                    ListItem listBListItem= listB.AddItem(new ListItemCreationInformation());
                                    listBListItem["Title"] = listData.Campaign;
                                    listBListItem["Study #"] = listData.StudyNum;
                                    listBListItem["Media"] = listData.Media;
                                    listBListItem.Update();
                                    clientContext.ExecuteQuery();                            
                            }
                        }
                    }
                }
            }
        }
    }

    Tuesday, September 20, 2016 10:01 AM
  • Hi Sunny,

    Launch the properties window of the App, and enable the App Installed, App Uninstalling and App Upgraded events as shown in the image below:

    This will automatically create an AppEventReceiver.svc file in the Services Folder of the Web Project.

    You can refer to the sample code inside the AppEventReceiver.svc.cs file shared by me aboveto attach the event receiver to the List A.

    Note: The endpoint url of the event receiver service file is different at the time of debugging using the Azure Service Bus and is different when the code is deployed in IIS.

    Assuming the name of the your Event Receiver Service file is RemoteEventReceiverTest.svc, below is the code snippet to dynamically get the Event Receiver Endpoint url:

     string remoteEventEndPointUrl = string.Empty;
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    // debugging url
                    string opContext = OperationContext.Current.Channel.LocalAddress.Uri.AbsoluteUri.Substring(
                      0, OperationContext.Current.Channel.LocalAddress.Uri.AbsoluteUri.LastIndexOf(Path.AltDirectorySeparatorChar));
    
                    remoteEventEndPointUrl = string.Format("{0}/RemoteEventReceiverTest.svc", opContext);
                }
                else
                {
                    HttpRequestMessageProperty requestProperty = (HttpRequestMessageProperty)OperationContext.Current.IncomingMessageProperties[HttpRequestMessageProperty.Name];
                    remoteEventEndPointUrl = string.Format("https://{0}/Services/RemoteEventReceiverTest.svc", requestProperty.Headers[HttpRequestHeader.Host]);               
                }


    Regards, Arcee Gomes

    Tuesday, September 20, 2016 10:37 AM
  • Hi Sunny,

    There are two ways to deploy your event receiver,

    1. You can deploy your remote event receiver by writing above code as Arcee Gomes mentioned above.

    2. When you publish your app you will get .WSP in app project BIN folder. You can directly upload that WSP file in solutions gallery Site Settings -> Web Designer Galleries -> Solutions and activate feature in desired sub site (Site Settings -> Site Actions -> Manage Site Features) . Remote event receiver gets attached with List Definition.

    Tuesday, September 20, 2016 12:02 PM
  • Hi Sunny Bahree,

    If the reply is helpful to you, you could mark the reply as answer. Thanks for your understanding.

    Best regards,

    Sara Fan


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, September 21, 2016 7:57 AM
    Moderator