Question

Is it possible to attach a Remote Event Receiver to my existing list in my site, and not to the list in my app? I'm guessing no as I can't find anything on this, and I think this would potentially 'hurt' SharePoint.

If I need to get this to work, I need to look at a sandbox or farm solution right?

TIA

Was it helpful?

Solution

I would say not possible to attach an Remote Event Receiver to a list in the Host-web, because these are actually Cross site-collections.

In the Host web you would only be able to deploy regular event receivers (targeting a class in an assembly) and the Remote event receivers take the URL of the actual WCF as the target, which wouldn't exist in the Host-web anyways:

<Url>~remoteAppUrl/RemoteEventReceiver1.svc</Url>

or

<Url>http://apps.mydomain.com:36511/MyReR.svc</Url>

OTHER TIPS

Yes, it is possible. Just handle the event receiver while installing the app by changing the property attribute to true as sown below.

enter image description here

And then just write the following code

public SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties)
        {
            SPRemoteEventResult result = new SPRemoteEventResult();

            switch (properties.EventType)
            {
                case SPRemoteEventType.AppInstalled:
                    HandleAppInstalled(properties);
                    break;
                case SPRemoteEventType.ItemAdded:
                    HandleItemAdded(properties);
                    break;
            }
            return result;
        }

        private void HandleAppInstalled(SPRemoteEventProperties properties)
        {
            using (ClientContext clientContext =
                      TokenHelper.CreateAppEventClientContext(properties, false))
            {
                if (clientContext != null)
                {
                    List myList = clientContext.Web.Lists.GetByTitle("Your List Name");
                    clientContext.Load(myList, p => p.EventReceivers);
                    clientContext.ExecuteQuery();
                    bool rerExists = false;

                    foreach (var rer in myList.EventReceivers)
                    {
                        if (rer.ReceiverName == "ItemAddedEvent")
                        {
                            rerExists = true;
                            System.Diagnostics.Trace.WriteLine("Found existing ItemAdded receiver at "
                               + rer.ReceiverUrl);
                        }
                    }
                    if (!rerExists)
                    {
                        EventReceiverDefinitionCreationInformation receiver =new EventReceiverDefinitionCreationInformation();
                        receiver.EventType = EventReceiverType.ItemAdded;


                        receiver.ReceiverUrl = "Your service url";
                        receiver.ReceiverName = "ItemAddedEvent";
                        receiver.Synchronization = EventReceiverSynchronization.Synchronous;
                        myList.EventReceivers.Add(receiver);
                        clientContext.ExecuteQuery();
                        System.Diagnostics.Trace.WriteLine("Added ItemAdded receiver at " + msg.Headers.To.ToString());
                    }
                }
            }
        }
 private void HandleItemAdded(SPRemoteEventProperties properties)
        {
            using (ClientContext clientContext =
              TokenHelper.CreateRemoteEventReceiverClientContext(properties))
            {
                if (clientContext != null)
                {
                    try
                    {
                        List photos = clientContext.Web.Lists.GetByTitle("Your List Name");
                        ListItem item = photos.GetItemById(
                           properties.ItemEventProperties.ListItemId);
                        clientContext.Load(item);
                        clientContext.ExecuteQuery();

                        item["Title"] += "\nUpdated by RER " +
                             System.DateTime.Now.ToLongTimeString();
                        item.Update();
                        clientContext.ExecuteQuery();
                    }
                    catch (Exception oops)
                    {
                        System.Diagnostics.Trace.WriteLine(oops.Message);
                    }
                }
            }
        }

I hope this will help you.

IT is possible only programmaticly but it does not work if you deploy.

var eventReceiver = new EventReceiverDefinitionCreationInformation
                     {
                         EventType = EventReceiverType.ItemAdding,
                         ReceiverAssembly = Assembly.GetExecutingAssembly().FullName,
                         ReceiverClass = "RemoteReciverWeb.Services.ContactAddEventReceiver",
                         ReceiverName = "ContactAddEventReceiver",
                         ReceiverUrl = remoteEventEndPointUrl,
                         SequenceNumber = 1000
                     };

clientContext.Load(eventresevers);
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top