Search code examples
c#exchangewebservices

EWS Getting all occurences from a master recurrence appointment


I use EWS FindAppointment to get all appointments between dates (see here for why Im using FindAppointments rather the FindItems)

// Set the start and end time and number of appointments to retrieve.
CalendarView cView = new CalendarView(start, end, 1000);
// Retrieve a collection of appointments by using the calendar view.
FindItemsResults<Appointment> findAppointmentResults = calendar.FindAppointments(cView);

I enumerate the results and create just a basic List of Appointments

Since FindAppointment does not return the Master for recurring meetings I use the AppointmentType.Occurrence appointments returned to get their Master

Appointment.BindToRecurringMaster(this.exchangeService, new ItemId(occurrence.Id.ToString()));

With this I form another List of all my Master Appointments.

I'm trying to use the Masters to retrieve every appointment in the series however thats where Im having trouble.
I tried to iterate through the list (as shown here, MSDN)

 public List<Appointment> getAllOccurrencesFromMaster(Appointment masterOccurrence) {
            List<Appointment> retVal = new List<Appointment>();


            int iCurrentIndex = 1;
            DateTime occurStart = masterOccurrence.Start;
            try {
                // Get the appointment that is located at the current index in the series.
                Appointment seriesAppt = Appointment.BindToOccurrence(this.exchangeService, new ItemId(masterOccurrence.Id.ToString()), iCurrentIndex);
                retVal.Add(seriesAppt);

                while (true) {
                    try {
                        Appointment appt = Appointment.BindToOccurrence(this.exchangeService, new ItemId(masterOccurrence.Id.ToString()), iCurrentIndex + 1);
                        retVal.Add(appt);
                    } catch (ServiceResponseException sre) {
                        Console.WriteLine("Occurrence with this index was previously deleted from the recurrence. " + sre.ToString());
                    } catch (Exception e) {
                            throw e;
                        }
                    }
                    iCurrentIndex++;
                }
            }catch (Exception e) {
                if (e.Message == "Occurrence index is out of recurrence range.") {
                    Console.WriteLine("The end of the series has been reached.");
                } else {
                    Console.WriteLine("bad error. " + e.ToString());
                    throw e;
                }
            }

            return retVal;
        }

My problem with the above code is that when a deleted item is reached it throws the same exception as when the index is out of range: ServiceResponseException.
Which is a problem because for one I want to continue and the other I want to break and Im not going to switch that based on the message text. And furthermore I need the deleted occurrences. How do I get all appointments in a recurring series?

I have also tried to use the DeletedOccurances property of the Master Appointment but none of the entries had their IDs populated. We do a Unidirectional sync from Exchange to our system, and I need to know whats deleted so I can remove it from our system. We keep track of the calenderItem.ID and so I need that when something is deleted to know which meeting to delete in our system.


Solution

  • Instead of getting all appointments in the series. I used the Master Recurrence appointment.recurrence object as well as deleted and modified occurrences to figure out whats going on with the appointment and properly sync on our side of things.