Creating SharePoint Online Remote Event Receiver

Creating a remote event receiver for a SharePoint Online site can be a frustrating experience. When it does not work there is very little diagnostic information to look at to figure out why its not firing.

I’m going to go over a brief check list that will hopefully prevent others spending hours on what should be a simple procedure

1. Make sure your ListTemplateId is correct.

In your Event Receivers XML you will see the line

<Receivers ListTemplateId=”106″>

It is important that your ListTemplateId reflects the type of list you need to attach an event receiver to. In this case it is a Calendar. A csutom list is 100. The full list can be found here: https://msdn.microsoft.com/en-us/library/dd958106(v=office.12).aspx

2. Make sure your App server has a valid SSL certificate. A self signed one will not work.

This is a catch I could not find anything about, however in my experience SharePoint Online will refuse to communicate to a Server if its SSL certificate is not valid.

3. Bind your Event Receiver.

Another common gotcha, event receivers must be bound and activated via code. It can be done via Powershell, or the CSOM. I prefer the latter and have written a small function to help me do this.

Usage:

eventReceiverUtility.UnBind(clientContext, "Master Timeline", "CalanderRecevierItemAdded");

eventReceiverUtility.Bind(SpContext.ClientContext, "Master Timeline", "CalanderRecevierItemAdded", "https://<url>/Services/CalanderRecevier.svc", 10000, EventReceiverType.ItemAdding);

The magic:

        public void Bind(ClientContext clientContext,
                        string listName,
                        string eventRecieverName,
                        string remoteUrl,
                        int sequencyId,
                        EventReceiverType eventType)
        {
            try
            {
                List olist = clientContext.Web.Lists.GetByTitle(listName);
                clientContext.Load(olist, o => o.EventReceivers);
                clientContext.ExecuteQuery();
                bool isReRExsist = false;
                foreach (var receiver in olist.EventReceivers)
                {
                    if (receiver.ReceiverName == eventRecieverName && receiver.EventType == eventType)
                    {

                        isReRExsist = true;
                        break;
                    }
                }
                if (!isReRExsist)
                {
                    EventReceiverDefinitionCreationInformation eventReDefCreation = new EventReceiverDefinitionCreationInformation()
                    {

                        EventType = eventType,
                        ReceiverAssembly = Assembly.GetExecutingAssembly().FullName,
                        ReceiverName = eventRecieverName,
                        ReceiverClass = eventRecieverName,
                        ReceiverUrl = remoteUrl,
                        SequenceNumber = sequencyId
                    };
                    olist.EventReceivers.Add(eventReDefCreation);
                    clientContext.ExecuteQuery();

                }
            }
            catch (Exception ex)
            {

            }
        }

        public void UnBind(ClientContext clientContext,
                string listName,
                string eventRecieverName)
        {
            try
            {
                var list = clientContext.Web.Lists.GetByTitle(listName);
                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 == eventRecieverName)
                    {
                        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();
                }
            }
            catch (Exception ex)
            {

            }
        }

    }

Related Posts

New SharePoint 2016 Features: Microsoft Flow App, PowerApps, and Updated Framework
Upsert function for Rest SharePoint API
Provider Hosted App Times Out
Web deployment task failed. http://go.microsoft.com/fwlink/?LinkId=221672#ERROR_CONNECTION_TERMINATED.

Leave a Comment