3

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

-1

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>
  • Yup, that's what I thought, just wanted a second opinion. Thanks. – David Lozzi May 30 '13 at 11:06
2

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.

  • I forgot to remove some code. Just remove the "+ msg.Headers.To.ToString()" portion from the code. – Trimantra Software Solution Mar 17 '16 at 12:23
  • Thank you, this worked for me. I actually used the ItemAdding event from an Appweb List instead of AppInstalling – Colbs Jul 20 '16 at 16:16
0

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.