kamagra how much to take

Adding Remote Event Receivers To An Exisiting List In Office 365

This is a generic approach in order to add event receivers to an existing list, say at the host or app web level really doesn’t matter. There are a couple of methods that make it nice and clean.

This first part will define a little helper method to build the creation information for the event receiver. Notice you have to specify the remote app url in your manifest tokens.

  1. public static EventReceiverDefinitionCreationInformation BuildGenericEventRecieverWithoutEventType()
  2. {
  3. string remoteAppurl = HttpContext.Current.Request["RemoteAppUrl"];
  4. string remoteEventEndPointUrl = String.Format("{0}/PutYourServiceNameHere.svc", remoteAppurl);
  5. EventReceiverDefinitionCreationInformation eventReceiver = new EventReceiverDefinitionCreationInformation();
  6. eventReceiver.ReceiverName = "GiveYourRevc";
  7. eventReceiver.ReceiverUrl = remoteEventEndPointUrl;
  8. eventReceiver.SequenceNumber = 5000;
  9. eventReceiver.ReceiverClass = "Fully qualified Path To RER Assembly";
  10. eventReceiver.ReceiverAssembly = Assembly.GetExecutingAssembly().FullName;
  11. return eventReceiver;
  12. }

If you are adding or removing a receiver, you of course have to test whether that receiver exists! That’s what the below method does. We just use the class name since that should be unique.

  1. public static bool DoesEventReceiverDefintionExistBasedOnCreationInfo(EventReceiverDefinitionCreationInformation info, List list)
  2. {
  3. bool exists = false;
  4. list.Context.Load(list, x => x.EventReceivers);
  5. list.Context.ExecuteQuery();
  6. foreach (EventReceiverDefinition eventReceiverDefinition in list.EventReceivers)
  7. {
  8. string innerDefId = eventReceiverDefinition.ReceiverClass;
  9. string outerDefId = info.ReceiverClass;
  10. if (innerDefId.Equals(outerDefId, StringComparison.InvariantCultureIgnoreCase))
  11. {
  12. exists = true;
  13. break;
  14. }
  15. }
  16. return exists;
  17. }

Since the receivers were built without a specific event type, you can specify as many receiver capture behaviors as you want, as demonstrated in the below example.

  1. public static void ApplyEventReceiver(ClientContext context, List list)
  2. {
  3. EventReceiverDefinitionCreationInformation eventReceiver = Helpers.BuildGenericEventRecieverWithoutEventType();
  4. eventReceiver.EventType = EventReceiverType.ItemAdded;
  5. if (!DoesEventReceiverDefintionExistBasedOnCreationInfo(eventReceiver, list))
  6. {
  7. list.EventReceivers.Add(eventReceiver);
  8. }
  9. eventReceiver = BuildGenericEventRecieverWithoutEventType();
  10. eventReceiver.EventType = EventReceiverType.ItemUpdated;
  11. if (!DoesEventReceiverDefintionExistBasedOnCreationInfo(eventReceiver, list))
  12. {
  13. list.EventReceivers.Add(eventReceiver);
  14. }
  15. eventReceiver = BuildGenericEventRecieverWithoutEventType();
  16. eventReceiver.EventType = EventReceiverType.ItemDeleted;
  17. if (!DoesEventReceiverDefintionExistBasedOnCreationInfo(eventReceiver, list))
  18. {
  19. list.EventReceivers.Add(eventReceiver);
  20. }
  21. eventReceiver = BuildGenericEventRecieverWithoutEventType();
  22. eventReceiver.EventType = EventReceiverType.ItemFileMoved;
  23. if (!DoesEventReceiverDefintionExistBasedOnCreationInfo(eventReceiver, list))
  24. {
  25. list.EventReceivers.Add(eventReceiver);
  26. }
  27. context.ExecuteQuery();
  28. }

Unapplying event receivers is pretty easy to, but remember you are modifying the collection so you have to ensure that the enumeration can execute! That is why you see the two LINQ chains.

  1. public static void UnApplyEventReceiver(ClientContext context, List list)
  2. {
  3. EventReceiverDefinitionCollection eventReceiverDefinitionCollection = list.EventReceivers;
  4. context.Load(eventReceiverDefinitionCollection);
  5. context.ExecuteQuery();
  6. List<Guid> ids = eventReceiverDefinitionCollection.Select(eventReceiverDefinition => eventReceiverDefinition.ReceiverId).ToList();
  7. foreach (EventReceiverDefinition definition in ids.Select(eventReceiverDefinitionCollection.GetById))
  8. {
  9. definition.DeleteObject();
  10. context.ExecuteQuery();
  11. }
  12. }
Share

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>