Question

I have written a remote event receiver to fire when item is deleting in a docmentlibarary. but itemDleting event is not firing.But itemAdded and ItemDeleted events are working ok.

in remote event receiver file

public void ProcessOneWayEvent(SPRemoteEventProperties properties)
        {


            if (properties.EventType == SPRemoteEventType.ItemDeleting)
            {
                using (ClientContext clientContext = TokenHelper.CreateRemoteEventReceiverClientContext(properties))
                {
                    if (clientContext != null)
                    {
                        try
                        {
                            DeleteDocuments(properties);
                        }
                        catch (Exception ex) { }
                    }
                }
            }

        }

in appeventreceiver file

 public SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties)
        {
            SPRemoteEventResult result = new SPRemoteEventResult();
            if (properties.EventType == SPRemoteEventType.AppInstalled)
            {
                using (ClientContext clientContext = TokenHelper.CreateAppEventClientContext(properties, false))
                {
                    if (clientContext != null)
                    {
                        //Get reference to the host web list with name Feedback
                        var documentsList = clientContext.Web.Lists.GetByTitle("Pre Meeting Docs");


                        clientContext.Load(documentsList);
                        clientContext.ExecuteQuery();
                        string remoteUrl = "https://xxxxx.net/Services/RemoteEventReceiver1.svc";



                        //Create the remote event receiver definition for deletion
                        EventReceiverDefinitionCreationInformation deleteEventReceiver = new EventReceiverDefinitionCreationInformation()
                        {
                            EventType = EventReceiverType.ItemDeleting,
                            ReceiverAssembly = Assembly.GetExecutingAssembly().FullName,
                            ReceiverName = "RemoteEventReceiver1",
                            ReceiverClass = "RemoteEventReceiver1",
                            ReceiverUrl = remoteUrl,
                            SequenceNumber = 15001
                        };


                        //Add the remote event receiver to the host web list

                        documentsList.EventReceivers.Add(deleteEventReceiver);
                        clientContext.ExecuteQuery();
                    }
                }
            }
            else if (properties.EventType == SPRemoteEventType.AppUninstalling)
            {
                using (ClientContext clientContext = TokenHelper.CreateAppEventClientContext(properties, false))
                {
                    var list = clientContext.Web.Lists.GetByTitle("Pre Meeting Docs");

                    clientContext.Load(list);
                    clientContext.ExecuteQuery();
                    EventReceiverDefinitionCollection erdc = list.EventReceivers;
                    clientContext.Load(erdc);
                    clientContext.ExecuteQuery();
                    List<EventReceiverDefinition> toDelete = new List<EventReceiverDefinition>();
                    foreach (EventReceiverDefinition erd in erdc)
                    {
                        if (erd.ReceiverName == "RemoteEventReceiver1")
                        {
                            toDelete.Add(erd);
                        }
                    }
                    //Delete the remote event receiver from the list, when the app gets uninstalled
                    foreach (EventReceiverDefinition item in toDelete)
                    {
                        item.DeleteObject();
                        clientContext.ExecuteQuery();
                    }
                }
            }
            return result;
        }

in elements.xml

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers ListTemplateId="3101"> 
    <Receiver>
      <Name>RemoteEventReceiver1ItemDeleting</Name>
      <Type>ItemDeleting</Type>
      <SequenceNumber>10000</SequenceNumber>
      <Url>~remoteAppUrl/Services/RemoteEventReceiver1.svc</Url>
    </Receiver>
  </Receivers>
</Elements>
Was it helpful?

Solution

If you want to use any of the "-ing" events, then write your codes in ProcessEvent().

ProcessEvent()

It handles "Before" events and returns an object to SharePoint that reports on whether it should cancel the current process or terminate it.

If you want to use any of the "-ing" events, then write your codes in ProcessEvent().

ProcessOneWayEvent()

It handles "After" events. It runs asynchronously and does not return anything to SharePoint.

If you are using any code which is to be run after the “-ed” event, then write your codes in ProcessOneWayEvent().

Check the article here: Difference Between Process Event() And Process One Way Event() In Event Receiver

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top