4

I have added itemUpdated functionality to my receiver but i notice that it overide my itemAdded function. whenever i add a new item, my RER is not triggered. But on editing RER is triggered.

EventReceiverDefinitionCreationInformation receiver = new EventReceiverDefinitionCreationInformation();
                receiver.EventType = EventReceiverType.ItemAdded;
                receiver.EventType=EventReceiverType.ItemUpdated;


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

                //Add the new event receiver to a list in the host web
                jobsList.EventReceivers.Add(receiver);
                clientContext.ExecuteQuery();
5

Attch both ItemAdded & ItemUpdated seperately.

var lstJob = clientContext.Web.Lists.GetByTitle("LISTNAME");
clientContext.Load(clientContext.Web);
clientContext.ExecuteQuery();

OperationContext op = OperationContext.Current;
Message msg = op.RequestContext.RequestMessage;

EventReceiverDefinitionCreationInformation addEventReceiver = new EventReceiverDefinitionCreationInformation()
{
    EventType = EventReceiverType.ItemAdded, /* ITEM ADDED*/
    ReceiverName = "RECEIVER_NAME",
    ReceiverUrl = msg.Headers.To.ToString()
};

lstJob.EventReceivers.Add(addEventReceiver);
clientContext.ExecuteQuery();

EventReceiverDefinitionCreationInformation updateEventReceiver = new EventReceiverDefinitionCreationInformation()
{
    EventType = EventReceiverType.ItemUpdated, /* ITEM UPDATE*/
    ReceiverName = "RECEIVER_NAME",
    ReceiverUrl = msg.Headers.To.ToString()
};

lstJob.EventReceivers.Add(updateEventReceiver);
clientContext.ExecuteQuery();
5

It's a normal behavior, where

You have set EventType to ItemAdded

receiver.EventType = EventReceiverType.ItemAdded;

Then in the next line, the EventType is set (overwrite) to ItemUpdated again!

receiver.EventType=EventReceiverType.ItemUpdated;

So the On ItemUpdated event only will work!


To overcome this issue, you should add each EventType to list independently as the following

public static void ApplyEventReceiver(ClientContext context, List list)
{
// First Event ItemAdded
EventReceiverDefinitionCreationInformation eventReceiver = Helpers.BuildGenericEventRecieverWithoutEventType();
eventReceiver.EventType = EventReceiverType.ItemAdded;
if (!DoesEventReceiverDefintionExistBasedOnCreationInfo(eventReceiver, list))
{
list.EventReceivers.Add(eventReceiver);
}

// Second Event ItemUpdated
eventReceiver = BuildGenericEventRecieverWithoutEventType();
eventReceiver.EventType = EventReceiverType.ItemUpdated;
if (!DoesEventReceiverDefintionExistBasedOnCreationInfo(eventReceiver, list))
{
list.EventReceivers.Add(eventReceiver);
}

context.ExecuteQuery();
}

Check also Adding Remote Event Receivers To An Existing List In Office 365

1

I have no idea why but only this work for me compare to the other answers.

 EventReceiverDefinitionCreationInformation receiver = new EventReceiverDefinitionCreationInformation();
            receiver.EventType = EventReceiverType.ItemAdded;



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

            //Add the new event receiver to a list in the host web
            jobsList.EventReceivers.Add(receiver);
            clientContext.ExecuteQuery();

            System.Diagnostics.Trace.WriteLine("Added ItemAdded receiver at " + receiver.ReceiverUrl);


            //add a second event receiver for item updated
            EventReceiverDefinitionCreationInformation receiverupdate = new EventReceiverDefinitionCreationInformation();
            receiverupdate.EventType = EventReceiverType.ItemUpdated;


            //Get WCF URL where this message was handled
            OperationContext opupdate = OperationContext.Current;
            Message msgupdate = opupdate.RequestContext.RequestMessage;
            receiverupdate.ReceiverUrl = msg.Headers.To.ToString();

            receiverupdate.ReceiverName = "Item Updated";
            receiverupdate.Synchronization = EventReceiverSynchronization.Synchronous;

            //Add the new event receiver to a list in the host web
            jobsList.EventReceivers.Add(receiverupdate);
            clientContext.ExecuteQuery();

            System.Diagnostics.Trace.WriteLine("Added ItemAdded receiver at " + receiver.ReceiverUrl);
  • 1
    Item updated event worked for you? You need to give your Event Receiver name correctly, against "receiverupdate.ReceiverName". – Vishnu S Oct 18 '17 at 8:56
  • 1
    Your answer is the same as what we have advised you In the above answers "you should add each EventType to list independently" !! also, you should clarify why the above answers are not working for you? – Mohamed El-Qassas MVP Oct 18 '17 at 9:17
  • @M.Qassas i have no idea why it wasnt working, the rer wasnt working at all. – Keshav Taurah Oct 18 '17 at 9:26

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.