0

I’m trying to work according to code in http://tinyurl.com/list-rer

For some reason the event is not caught. I did manage to catch ItemAdded event

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

namespace RERTestWeb.Services
{
    public class AppEventReceiver : IRemoteEventService
    {

private const string RECEIVER_NAME = "ListAddedEvent";

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

            switch (properties.EventType)
            {
                case SPRemoteEventType.AppInstalled:
                    HandleAppInstalled(properties);
                    break;
                case SPRemoteEventType.AppUninstalling:
                    HandleAppUninstalling(properties);
                    break;
                case SPRemoteEventType.ListAdded:
                    HandleListAdded(properties);
                    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();
        }

        private void HandleAppInstalled(SPRemoteEventProperties properties)
        {
            using (ClientContext cc = TokenHelper.CreateAppEventClientContext(properties, false))
            {
                if (cc != null)
                {
                    bool rerExists = false;
                    cc.Load(cc.Web.EventReceivers);
                    cc.ExecuteQuery();

                    foreach (var rer in cc.Web.EventReceivers)
                    {
                        if (rer.ReceiverName == RECEIVER_NAME)
                        {
                            rerExists = true;
                            System.Diagnostics.Trace.WriteLine("Found existing ListAdded receiver at " + rer.ReceiverUrl);
                        }
                    }

                    if (!rerExists)
                    {
                        EventReceiverDefinitionCreationInformation receiver = new EventReceiverDefinitionCreationInformation();
                        receiver.EventType = EventReceiverType.ListAdded;

                        //Get WCF URL where this message was handled
                        OperationContext op = OperationContext.Current;
                        Message msg = op.RequestContext.RequestMessage;
                        receiver.ReceiverUrl = msg.Headers.To.ToString();
                        receiver.ReceiverName = RECEIVER_NAME;
                        receiver.Synchronization = EventReceiverSynchronization.Synchronous;

                        cc.Web.EventReceivers.Add(receiver);
                        cc.ExecuteQuery();
                        System.Diagnostics.Trace.WriteLine("Added ListAdded receiver at " + msg.Headers.To.ToString());
                    }

                }
            }
        }

        private void HandleAppUninstalling(SPRemoteEventProperties properties)
        {
            using (ClientContext cc = TokenHelper.CreateAppEventClientContext(properties, false))
            {
                if (cc != null)
                {
                    cc.Load(cc.Web.EventReceivers);
                    cc.ExecuteQuery();
                    var rer = cc.Web.EventReceivers.Where(e => e.ReceiverName == RECEIVER_NAME).FirstOrDefault();

                    try
                    {
                        System.Diagnostics.Trace.WriteLine("Removing ListAdded receiver at " + rer.ReceiverUrl);

                        rer.DeleteObject();
                        cc.ExecuteQuery();
                    }
                    catch(Exception ex)
                    {
                        System.Diagnostics.Trace.WriteLine(ex.Message);
                    }
                }
            }
        }


        private void HandleListAdded(SPRemoteEventProperties properties)
        {
            using (ClientContext cc = TokenHelper.CreateRemoteEventReceiverClientContext(properties))
            {
                if (cc != null)
                {
                    try
                    {
                        if (properties.ListEventProperties.TemplateId == (int)ListTemplateType.DocumentLibrary)
                        {
                            // do something
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Trace.WriteLine(ex.Message);
                    }
                }

            }

        }

    }
}        
  • Can you please explain which event is not caught (or not working) – Amal Hashim Jan 20 '15 at 15:13
  • the HandleListAdded() event – daniel8538 Jan 20 '15 at 15:14
  • Ok I dont see any action in case of ListAdded. How you figured the code is not executing? – Amal Hashim Jan 20 '15 at 15:16
  • Yes. That function is doing nothing? I mean that function has a simple IF statement and inside the block no code. – Amal Hashim Jan 20 '15 at 15:19
  • in the switch there is function call in case of a SPRemoteEventType.ListAdded event. when i put a break-point in the using inside the function and debug, it does not break. even if i put code in the function. i left it empty for demonstration purposes. – daniel8538 Jan 20 '15 at 15:20

Your Answer

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

Browse other questions tagged or ask your own question.