Beispiel #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="addedAppointments"></param>
        /// <param name="existingAppointments"></param>
        /// <returns></returns>
        private List <Appointment> UpdateWithChildId(CalendarAppointments addedAppointments,
                                                     CalendarAppointments existingAppointments)
        {
            //Add appointments to update
            var updateList = new List <Appointment>();

            foreach (var appointment in addedAppointments)
            {
                var presentAppointment = existingAppointments.FirstOrDefault(t => t.CompareSourceId(appointment));
                if (presentAppointment != null)
                {
                    var childKey = appointment.GetChildEntryKey();
                    if (!presentAppointment.ExtendedProperties.ContainsKey(childKey))
                    {
                        presentAppointment.ExtendedProperties.Add(childKey, appointment.AppointmentId);
                    }
                    else
                    {
                        presentAppointment.ExtendedProperties[childKey] = appointment.AppointmentId;
                    }
                    updateList.Add(presentAppointment);
                }
            }
            return(updateList);
        }
 public bool GetDestEntriesToDelete(CalendarSyncProfile syncProfile, CalendarAppointments sourceList,
     CalendarAppointments destinationList)
 {
     EvaluateAppointmentsToDelete(syncProfile, sourceList, destinationList, DestAppointmentsToDelete,
         DestAppointmentsToUpdate, SourceAppointmentsToUpdate, DestOrphanEntries);
     return true;
 }
 public bool GetDestEntriesToDelete(CalendarSyncProfile syncProfile, CalendarAppointments sourceList,
                                    CalendarAppointments destinationList)
 {
     EvaluateAppointmentsToDelete(syncProfile, sourceList, destinationList, DestAppointmentsToDelete,
                                  DestAppointmentsToUpdate, SourceAppointmentsToUpdate, DestOrphanEntries);
     return(true);
 }
        public async Task <CalendarAppointments> AddCalendarEvents(List <Appointment> calendarAppointments,
                                                                   bool addDescription,
                                                                   bool addReminder, bool addAttendees, bool attendeesToDescription,
                                                                   IDictionary <string, object> calendarSpecificData)
        {
            var addedAppointments = new CalendarAppointments();

            if (!calendarAppointments.Any())
            {
                addedAppointments.IsSuccess = true;
                return(addedAppointments);
            }

            CheckCalendarSpecificData(calendarSpecificData);

            var errorList = new Dictionary <int, Appointment>();
            //Get Calendar Service
            var calendarService = GetCalendarService(AccountName);

            if (calendarAppointments == null || string.IsNullOrEmpty(CalendarId))
            {
                addedAppointments.IsSuccess = false;
                return(addedAppointments);
            }
            try
            {
                if (calendarAppointments.Any())
                {
                    //Split the list of calendarAppointments by 1000 per list
                    var appts =
                        await AddCalendarEventsInternal(calendarAppointments, addDescription, addReminder, addAttendees,
                                                        attendeesToDescription, calendarService, errorList);

                    addedAppointments.AddRange(appts);
                    if (errorList.Count > 0)
                    {
                        var remaningList = errorList.Select(CreateAppointmentWithoutAttendees).ToList();
                        errorList.Clear();

                        appts = await AddCalendarEventsInternal(remaningList, addDescription, addReminder, addAttendees,
                                                                attendeesToDescription, calendarService, errorList);

                        addedAppointments.AddRange(appts);
                    }
                }
            }
            catch (Exception exception)
            {
                Logger.Error(exception);
                addedAppointments.IsSuccess = false;
                return(addedAppointments);
            }
            addedAppointments.IsSuccess = true;
            return(addedAppointments);
        }
Beispiel #5
0
        public async Task <CalendarAppointments> GetCalendarEventsInRangeAsync(DateTime startDate, DateTime endDate,
                                                                               IDictionary <string, object> calendarSpecificData)
        {
            CheckCalendarSpecificData(calendarSpecificData);


            var service = GetExchangeService(ExchangeServerSettings);

            var calendarview = new CalendarView(startDate, endDate);

            // Get Default Calendar
            var outlookAppointments  = new CalendarAppointments();
            var exchangeAppointments = service.FindAppointments(_ewsCalendar.EntryId,
                                                                calendarview);


            service.LoadPropertiesForItems(
                from Item item in exchangeAppointments select item,
                new PropertySet(BasePropertySet.FirstClassProperties)
            {
                RequestedBodyType = BodyType.Text
            });

            if (exchangeAppointments != null)
            {
                foreach (var exchangeAppointment in exchangeAppointments)
                {
                    var appointment = new AppAppointment(exchangeAppointment.Body, exchangeAppointment.Location,
                                                         exchangeAppointment.Subject, exchangeAppointment.Start, exchangeAppointment.Start)
                    {
                        AppointmentId              = exchangeAppointment.Id.UniqueId,
                        AllDayEvent                = exchangeAppointment.IsAllDayEvent,
                        OptionalAttendees          = GetAttendees(exchangeAppointment.OptionalAttendees),
                        ReminderMinutesBeforeStart = exchangeAppointment.ReminderMinutesBeforeStart,
                        Organizer =
                            new Recipient
                        {
                            Name  = exchangeAppointment.Organizer.Name,
                            Email = exchangeAppointment.Organizer.Address
                        },
                        ReminderSet       = exchangeAppointment.IsReminderSet,
                        RequiredAttendees = GetAttendees(exchangeAppointment.RequiredAttendees)
                    };
                    outlookAppointments.Add(appointment);
                }
            }
            return(outlookAppointments);
        }
        public async Task<CalendarAppointments> UpdateCalendarEvents(List<Appointment> calendarAppointments, bool addDescription,
            bool addReminder, bool addAttendees, bool attendeesToDescription,
            IDictionary<string, object> calendarSpecificData)
        {
            var updateAppointments = new CalendarAppointments();
            if (!calendarAppointments.Any())
            {
                updateAppointments.IsSuccess = true;
                return updateAppointments;
            }
            CheckCalendarSpecificData(calendarSpecificData);

            var result = await
                Task<bool>.Factory.StartNew(
                    () =>
                        UpdateEvents(calendarAppointments, addDescription, addReminder, addAttendees,
                            attendeesToDescription, updateAppointments));
            updateAppointments.IsSuccess = result;
            return updateAppointments;
        }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="addedAppointments"></param>
 /// <param name="existingAppointments"></param>
 /// <returns></returns>
 private List<Appointment> UpdateWithChildId(CalendarAppointments addedAppointments,
     CalendarAppointments existingAppointments)
 {
     //Add appointments to update
     var updateList = new List<Appointment>();
     foreach (var appointment in addedAppointments)
     {
         var presentAppointment = existingAppointments.FirstOrDefault(t => t.CompareSourceId(appointment));
         if (presentAppointment != null)
         {
             var childKey = appointment.GetChildEntryKey();
             if (!presentAppointment.ExtendedProperties.ContainsKey(childKey))
             {
                 presentAppointment.ExtendedProperties.Add(childKey, appointment.AppointmentId);
             }
             else
             {
                 presentAppointment.ExtendedProperties[childKey] = appointment.AppointmentId;
             }
             updateList.Add(presentAppointment);
         }
     }
     return updateList;
 }
 public bool GetSourceEntriesToAdd(CalendarSyncProfile syncProfile, CalendarAppointments sourceList,
     CalendarAppointments destinationList)
 {
     EvaluateAppointmentsToAdd(syncProfile, destinationList, sourceList, SourceAppointmentsToAdd);
     return true;
 }
        public async Task<CalendarAppointments> AddCalendarEvents(List<Appointment> calendarAppointments,
            bool addDescription,
            bool addReminder, bool addAttendees, bool attendeesToDescription,
            IDictionary<string, object> calendarSpecificData)
        {
            var addedAppointments = new CalendarAppointments();
            if (!calendarAppointments.Any())
            {
                addedAppointments.IsSuccess = true;
                return addedAppointments;
            }

            CheckCalendarSpecificData(calendarSpecificData);

            var errorList = new Dictionary<int, Appointment>();
            //Get Calendar Service
            var calendarService = GetCalendarService(AccountName);

            if (calendarAppointments == null || string.IsNullOrEmpty(CalendarId))
            {
                addedAppointments.IsSuccess = false;
                return addedAppointments;
            }
            try
            {
                if (calendarAppointments.Any())
                {
                    //Split the list of calendarAppointments by 1000 per list
                    var appts =
                        await AddCalendarEventsInternal(calendarAppointments, addDescription, addReminder, addAttendees,
                            attendeesToDescription, calendarService, errorList);
                    addedAppointments.AddRange(appts);
                    if (errorList.Count > 0)
                    {
                        var remaningList = errorList.Select(CreateAppointmentWithoutAttendees).ToList();
                        errorList.Clear();

                        appts = await AddCalendarEventsInternal(remaningList, addDescription, addReminder, addAttendees,
                            attendeesToDescription, calendarService, errorList);
                        addedAppointments.AddRange(appts);
                    }
                }
            }
            catch (Exception exception)
            {
                Logger.Error(exception);
                addedAppointments.IsSuccess = false;
                return addedAppointments;
            }
            addedAppointments.IsSuccess = true;
            return addedAppointments;
        }
 public bool GetDestEntriesToAdd(CalendarSyncProfile syncProfile, CalendarAppointments sourceList,
                                 CalendarAppointments destinationList)
 {
     EvaluateAppointmentsToAdd(syncProfile, sourceList, destinationList, DestAppointmentsToAdd);
     return(true);
 }
        public async Task <CalendarAppointments> DeleteCalendarEvents(List <Appointment> calendarAppointments,
                                                                      IDictionary <string, object> calendarSpecificData)
        {
            var deletedAppointments = new CalendarAppointments();

            if (!calendarAppointments.Any())
            {
                deletedAppointments.IsSuccess = true;
                return(deletedAppointments);
            }

            CheckCalendarSpecificData(calendarSpecificData);

            var errorList = new Dictionary <int, Appointment>();
            //Get Calendar Service
            var calendarService = GetCalendarService(AccountName);

            if (calendarAppointments == null || string.IsNullOrEmpty(CalendarId))
            {
                deletedAppointments.IsSuccess = false;
                return(deletedAppointments);
            }

            try
            {
                if (calendarAppointments.Any())
                {
                    //Create a Batch Request
                    var batchRequest = new BatchRequest(calendarService);

                    //Split the list of calendarAppointments by 1000 per list
                    //Iterate over each appointment to create a event and batch it
                    for (var i = 0; i < calendarAppointments.Count; i++)
                    {
                        if (i != 0 && i % 999 == 0)
                        {
                            await batchRequest.ExecuteAsync();

                            batchRequest = new BatchRequest(calendarService);
                        }

                        var appointment   = calendarAppointments[i];
                        var deleteRequest = calendarService.Events.Delete(CalendarId,
                                                                          appointment.AppointmentId);
                        batchRequest.Queue <Event>(deleteRequest,
                                                   (content, error, index, message) =>
                                                   CallbackEventErrorMessage(content, error, index, message, calendarAppointments,
                                                                             "Error in deleting events", errorList, deletedAppointments));
                    }
                    await batchRequest.ExecuteAsync();
                }
            }
            catch (Exception exception)
            {
                Logger.Error(exception);
                deletedAppointments.IsSuccess = false;
                return(deletedAppointments);
            }
            deletedAppointments.IsSuccess = true;
            return(deletedAppointments);
        }
        public async Task<CalendarAppointments> GetCalendarEventsInRangeAsync(DateTime startDate, DateTime endDate,
            IDictionary<string, object> calendarSpecificData)
        {
            CheckCalendarSpecificData(calendarSpecificData);

            //Get Calendar Service
            var calendarService = GetCalendarService(AccountName);

            var finalEventList = new List<Appointment>();

            Events result = null;

            var eventListRequest = calendarService.Events.List(CalendarId);

            // Add Filters to event List Request
            eventListRequest.TimeMin = startDate;
            eventListRequest.TimeMax = endDate;
            eventListRequest.MaxAttendees = 1000;
            eventListRequest.SingleEvents = true;
            try
            {
                result = eventListRequest.Execute();
                if (result != null)
                {
                    while (result.Items != null)
                    {
                        // Add events to list, Split recurring appointments
                        foreach (var eventItem in result.Items)
                        {
                            if (eventItem.Status == "cancelled")
                            {
                                continue;
                            }

                            var appointment = CreateAppointment(eventItem);
                            finalEventList.Add(appointment);
                        }

                        //If all pages are over break
                        if (result.NextPageToken == null)
                        {
                            break;
                        }

                        //Set the next page to pull from request
                        eventListRequest.PageToken = result.NextPageToken;

                        result = await eventListRequest.ExecuteAsync();
                    }
                }
                else
                {
                    return null;
                }
            }
            catch (GoogleApiException exception)
            {
                Logger.Error(exception);
                return null;
            }
            catch (Exception exception)
            {
                Logger.Error(exception);
                return null;
            }

            var calendarAppointments = new CalendarAppointments { CalendarId = CalendarId };
            calendarAppointments.AddRange(finalEventList);
            return calendarAppointments;
        }
        public async Task<CalendarAppointments> UpdateCalendarEvents(List<Appointment> calendarAppointments, bool addDescription,
            bool addReminder, bool addAttendees, bool attendeesToDescription,
            IDictionary<string, object> calendarSpecificData)
        {
            var updatedAppointments = new CalendarAppointments();
            if (!calendarAppointments.Any())
            {
                updatedAppointments.IsSuccess = true;
                return updatedAppointments;
            }

            CheckCalendarSpecificData(calendarSpecificData);

            var errorList = new Dictionary<int, Appointment>();
            //Get Calendar Service
            var calendarService = GetCalendarService(AccountName);

            if (calendarAppointments == null || string.IsNullOrEmpty(CalendarId))
            {
                updatedAppointments.IsSuccess = false;
                return updatedAppointments;
            }

            try
            {
                if (calendarAppointments.Any())
                {
                    //Create a Batch Request
                    var batchRequest = new BatchRequest(calendarService);

                    //Split the list of calendarAppointments by 1000 per list

                    //Iterate over each appointment to create a event and batch it 
                    for (var i = 0; i < calendarAppointments.Count; i++)
                    {
                        if (i != 0 && i % 999 == 0)
                        {
                            await batchRequest.ExecuteAsync();
                            batchRequest = new BatchRequest(calendarService);
                        }

                        var appointment = calendarAppointments[i];
                        var calendarEvent = CreateUpdatedGoogleCalendarEvent(appointment, addDescription, addReminder,
                            addAttendees, attendeesToDescription);
                        var updateRequest = calendarService.Events.Update(calendarEvent,
                            CalendarId, calendarEvent.Id);
                        updateRequest.SendNotifications = false;
                        batchRequest.Queue<Event>(updateRequest,
                            (content, error, index, message) =>
                                CallbackEventErrorMessage(content, error, index, message, calendarAppointments, "Error in updating event",errorList,updatedAppointments));
                    }

                    await batchRequest.ExecuteAsync();
                }
            }
            catch (Exception exception)
            {
                Logger.Error(exception);
                updatedAppointments.IsSuccess = false;
                return updatedAppointments;
            }
            updatedAppointments.IsSuccess = true;
            return updatedAppointments;
        }
        public async Task<CalendarAppointments> DeleteCalendarEvents(List<Appointment> calendarAppointments,
            IDictionary<string, object> calendarSpecificData)
        {
            var deletedAppointments = new CalendarAppointments();
            if (!calendarAppointments.Any())
            {
                deletedAppointments.IsSuccess = true;
                return deletedAppointments;
            }

            CheckCalendarSpecificData(calendarSpecificData);

            var errorList = new Dictionary<int, Appointment>();
            //Get Calendar Service
            var calendarService = GetCalendarService(AccountName);

            if (calendarAppointments == null || string.IsNullOrEmpty(CalendarId))
            {
                deletedAppointments.IsSuccess = false;
                return deletedAppointments;
            }

            try
            {
                if (calendarAppointments.Any())
                {
                    //Create a Batch Request
                    var batchRequest = new BatchRequest(calendarService);

                    //Split the list of calendarAppointments by 1000 per list
                    //Iterate over each appointment to create a event and batch it 
                    for (var i = 0; i < calendarAppointments.Count; i++)
                    {
                        if (i != 0 && i % 999 == 0)
                        {
                            await batchRequest.ExecuteAsync();
                            batchRequest = new BatchRequest(calendarService);
                        }

                        var appointment = calendarAppointments[i];
                        var deleteRequest = calendarService.Events.Delete(CalendarId,
                            appointment.AppointmentId);
                        batchRequest.Queue<Event>(deleteRequest,
                            (content, error, index, message) =>
                                CallbackEventErrorMessage(content, error, index, message, calendarAppointments, 
                                "Error in deleting events",errorList,deletedAppointments));
                    }
                    await batchRequest.ExecuteAsync();
                }
            }
            catch (Exception exception)
            {
                Logger.Error(exception);
                deletedAppointments.IsSuccess = false;
                return deletedAppointments;
            }
            deletedAppointments.IsSuccess = true;
            return deletedAppointments;
        }
        public async Task<CalendarAppointments> GetCalendarEventsInRangeAsync(DateTime startDate, DateTime endDate,
            IDictionary<string, object> calendarSpecificData)
        {
            CheckCalendarSpecificData(calendarSpecificData);
            var calendarAppointments = new CalendarAppointments();

            var appointmentList =
                await
                    Task<List<Appointment>>.Factory.StartNew(
                        () => GetAppointments(startDate, endDate));

            if (appointmentList == null)
            {
                return null;
            }

            if (OutlookCalendar != null)
            {
                calendarAppointments.CalendarId = OutlookCalendar.EntryId;
            }

            calendarAppointments.AddRange(appointmentList);

            return calendarAppointments;
        }
 /// <summary>
 /// </summary>
 /// <param name="calendarAppointments"></param>
 /// <param name="calendarSpecificData"></param>
 /// <returns>
 /// </returns>
 public async Task<CalendarAppointments> DeleteCalendarEvents(List<Appointment> calendarAppointments,
     IDictionary<string, object> calendarSpecificData)
 {
     var deleteAppointments = new CalendarAppointments();
     if (!calendarAppointments.Any())
     {
         deleteAppointments.IsSuccess = true;
         return deleteAppointments;
     }
     CheckCalendarSpecificData(calendarSpecificData);
     var result = await 
         Task<bool>.Factory.StartNew(() =>
             DeleteEvents(calendarAppointments, deleteAppointments));
     
     deleteAppointments.IsSuccess = result;
     return deleteAppointments;
 }
        public async Task <CalendarAppointments> UpdateCalendarEvents(List <Appointment> calendarAppointments, bool addDescription,
                                                                      bool addReminder, bool addAttendees, bool attendeesToDescription,
                                                                      IDictionary <string, object> calendarSpecificData)
        {
            var updatedAppointments = new CalendarAppointments();

            if (!calendarAppointments.Any())
            {
                updatedAppointments.IsSuccess = true;
                return(updatedAppointments);
            }

            CheckCalendarSpecificData(calendarSpecificData);

            var errorList = new Dictionary <int, Appointment>();
            //Get Calendar Service
            var calendarService = GetCalendarService(AccountName);

            if (calendarAppointments == null || string.IsNullOrEmpty(CalendarId))
            {
                updatedAppointments.IsSuccess = false;
                return(updatedAppointments);
            }

            try
            {
                if (calendarAppointments.Any())
                {
                    //Create a Batch Request
                    var batchRequest = new BatchRequest(calendarService);

                    //Split the list of calendarAppointments by 1000 per list

                    //Iterate over each appointment to create a event and batch it
                    for (var i = 0; i < calendarAppointments.Count; i++)
                    {
                        if (i != 0 && i % 999 == 0)
                        {
                            await batchRequest.ExecuteAsync();

                            batchRequest = new BatchRequest(calendarService);
                        }

                        var appointment   = calendarAppointments[i];
                        var calendarEvent = CreateUpdatedGoogleCalendarEvent(appointment, addDescription, addReminder,
                                                                             addAttendees, attendeesToDescription);
                        var updateRequest = calendarService.Events.Update(calendarEvent,
                                                                          CalendarId, calendarEvent.Id);
                        updateRequest.SendNotifications = false;
                        batchRequest.Queue <Event>(updateRequest,
                                                   (content, error, index, message) =>
                                                   CallbackEventErrorMessage(content, error, index, message, calendarAppointments, "Error in updating event", errorList, updatedAppointments));
                    }

                    await batchRequest.ExecuteAsync();
                }
            }
            catch (Exception exception)
            {
                Logger.Error(exception);
                updatedAppointments.IsSuccess = false;
                return(updatedAppointments);
            }
            updatedAppointments.IsSuccess = true;
            return(updatedAppointments);
        }
        public async Task <CalendarAppointments> GetCalendarEventsInRangeAsync(DateTime startDate, DateTime endDate,
                                                                               IDictionary <string, object> calendarSpecificData)
        {
            CheckCalendarSpecificData(calendarSpecificData);

            //Get Calendar Service
            var calendarService = GetCalendarService(AccountName);

            var finalEventList = new List <Appointment>();

            Events result = null;

            var eventListRequest = calendarService.Events.List(CalendarId);

            // Add Filters to event List Request
            eventListRequest.TimeMin      = startDate;
            eventListRequest.TimeMax      = endDate;
            eventListRequest.MaxAttendees = 1000;
            eventListRequest.SingleEvents = true;
            try
            {
                result = eventListRequest.Execute();
                if (result != null)
                {
                    while (result.Items != null)
                    {
                        // Add events to list, Split recurring appointments
                        foreach (var eventItem in result.Items)
                        {
                            if (eventItem.Status == "cancelled")
                            {
                                continue;
                            }

                            var appointment = CreateAppointment(eventItem);
                            finalEventList.Add(appointment);
                        }

                        //If all pages are over break
                        if (result.NextPageToken == null)
                        {
                            break;
                        }

                        //Set the next page to pull from request
                        eventListRequest.PageToken = result.NextPageToken;

                        result = await eventListRequest.ExecuteAsync();
                    }
                }
                else
                {
                    return(null);
                }
            }
            catch (GoogleApiException exception)
            {
                Logger.Error(exception);
                return(null);
            }
            catch (Exception exception)
            {
                Logger.Error(exception);
                return(null);
            }

            var calendarAppointments = new CalendarAppointments {
                CalendarId = CalendarId
            };

            calendarAppointments.AddRange(finalEventList);
            return(calendarAppointments);
        }
        /// <summary>
        /// </summary>
        /// <param name="syncProfile"></param>
        /// <param name="sourceList"></param>
        /// <param name="destinationList"></param>
        /// <param name="destAppointmentsToDelete"></param>
        /// <param name="destAppointmentsToUpdate"></param>
        /// <param name="sourceAppointmentsToUpdate"></param>
        /// <param name="destOrphanEntries"></param>
        /// <returns>
        /// </returns>
        private void EvaluateAppointmentsToDelete(CalendarSyncProfile syncProfile,
            CalendarAppointments sourceList, CalendarAppointments destinationList,
            List<Appointment> destAppointmentsToDelete,
            List<Appointment> destAppointmentsToUpdate, List<Appointment> sourceAppointmentsToUpdate,
            List<Appointment> destOrphanEntries)
        {
            var addDescription =
                syncProfile.CalendarEntryOptions.HasFlag(CalendarEntryOptionsEnum.Description);
            var addReminders =
                syncProfile.CalendarEntryOptions.HasFlag(CalendarEntryOptionsEnum.Reminders);
            var addAttendeesToDescription =
                syncProfile.CalendarEntryOptions.HasFlag(CalendarEntryOptionsEnum.AttendeesToDescription);

            if (!destinationList.Any())
            {
                foreach (var appointment in sourceList)
                {
                    if (appointment.ChildId != null)
                    {
                        var key = AppointmentHelper.GetChildEntryKey(sourceList.CalendarId);
                        if (!appointment.ExtendedProperties.ContainsKey(key))
                        {
                            appointment.ExtendedProperties.Remove(key);
                        }
                        sourceAppointmentsToUpdate.AddCompareForUpdate(appointment);
                    }
                }
                return;
            }

            foreach (var destAppointment in destinationList)
            {
                //If SourceId is null, it is not a copy of any entry from the selected source calendar
                if (destAppointment.SourceId == null)
                {
                    if (syncProfile.SyncSettings.SyncMode == SyncModeEnum.OneWay)
                    {
                        //If mode is one way & user has disabled delete, do not remove this entry, as this is an original entry in the calendar
                        //Else this entry is not a copy of any appointment in source calendar so delete it
                        destOrphanEntries.Add(destAppointment);
                    }
                    else
                    {
                        if (destAppointment.ChildId == null)
                        {
                            var childAppointment = sourceList.FirstOrDefault(t => destAppointment.CompareSourceId(t));
                            if (childAppointment != null)
                            {
                                destAppointment.ChildId = childAppointment.AppointmentId;
                                var key = childAppointment.GetChildEntryKey();
                                if (!destAppointment.ExtendedProperties.ContainsKey(key))
                                {
                                    destAppointment.ExtendedProperties.Add(key, childAppointment.AppointmentId);
                                }
                                else
                                {
                                    destAppointment.ExtendedProperties[key] = childAppointment.AppointmentId;
                                }
                                destAppointmentsToUpdate.AddCompareForUpdate(destAppointment);
                            }
                        }
                        else if (syncProfile.SyncSettings.KeepLastModifiedVersion)
                        {
                            var childAppointment =
                                sourceList.FirstOrDefault(t => t.AppointmentId.Equals(destAppointment.ChildId));
                            if (childAppointment == null)
                            {
                                destAppointmentsToDelete.Add(destAppointment);
                            }
                        }
                    }
                }
                else
                {
                    //If the mode is two way, look for its parent copy in Source calendar
                    Appointment sourceAppointment;
                    if (syncProfile.SyncSettings.SyncMode == SyncModeEnum.TwoWay
                        && syncProfile.SyncSettings.KeepLastModifiedVersion)
                    {
                        //If no entry was found, it is original entry of the calendar, Ignore
                        //If a child entry is found in source calendar, compare
                        sourceAppointment = sourceList.FirstOrDefault(t => t.CompareSourceId(destAppointment));
                        if (sourceAppointment != null)
                        {
                            //If any entry is found in source appointment and its contents are not equal to source appointment,
                            //If an entry is found and i same, ignore
                            if (!CompareAppointments(destAppointment, sourceAppointment, addDescription,
                                addReminders, addAttendeesToDescription))
                            {
                                if (sourceAppointment.LastModified.HasValue && destAppointment.LastModified.HasValue)
                                {
                                    if (destAppointment.LastModified.GetValueOrDefault() >
                                        sourceAppointment.LastModified.GetValueOrDefault())
                                    {
                                        sourceAppointment.CopyDetail(destAppointment,
                                            syncProfile.CalendarEntryOptions);
                                        sourceAppointmentsToUpdate.AddCompareForUpdate(sourceAppointment);
                                        continue;
                                    }
                                }
                                //Destination Calendar Entry is not Matching its Source Calendar Entry, Update it
                                destAppointment.CopyDetail(sourceAppointment, syncProfile.CalendarEntryOptions);
                                destAppointmentsToUpdate.AddCompareForUpdate(destAppointment);
                                continue;
                            }
                        }
                    }

                    //If source appointment is not null, means it is a copy of an existing entry in Source calendar
                    sourceAppointment = sourceList.FirstOrDefault(t => t.CompareSourceId(destAppointment));
                    if (sourceAppointment != null)
                    {
                        //If any entry is found in source appointment and its contents are not equal to source appointment
                        if (!CompareAppointments(destAppointment, sourceAppointment, addDescription, addReminders,
                            addAttendeesToDescription))
                        {
                            destAppointment.CopyDetail(sourceAppointment, syncProfile.CalendarEntryOptions);
                            destAppointmentsToUpdate.AddCompareForUpdate(destAppointment);
                        }
                    }
                    else
                    {
                        //No parent entry is found, delete it
                        sourceAppointment = sourceList.FirstOrDefault(t =>
                            CompareAppointments(destAppointment, t, addDescription, addReminders,
                                addAttendeesToDescription));
                        if (sourceAppointment == null)
                        {
                            //If parent entry isn't found
                            destAppointmentsToDelete.Add(destAppointment);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// </summary>
        /// <param name="syncProfile"></param>
        /// <param name="sourceList"></param>
        /// <param name="destinationList"></param>
        /// <param name="destAppointmentsToDelete"></param>
        /// <param name="destAppointmentsToUpdate"></param>
        /// <param name="sourceAppointmentsToUpdate"></param>
        /// <param name="destOrphanEntries"></param>
        /// <returns>
        /// </returns>
        private void EvaluateAppointmentsToDelete(CalendarSyncProfile syncProfile,
                                                  CalendarAppointments sourceList, CalendarAppointments destinationList,
                                                  List <Appointment> destAppointmentsToDelete,
                                                  List <Appointment> destAppointmentsToUpdate, List <Appointment> sourceAppointmentsToUpdate,
                                                  List <Appointment> destOrphanEntries)
        {
            var addDescription =
                syncProfile.CalendarEntryOptions.HasFlag(CalendarEntryOptionsEnum.Description);
            var addReminders =
                syncProfile.CalendarEntryOptions.HasFlag(CalendarEntryOptionsEnum.Reminders);
            var addAttendeesToDescription =
                syncProfile.CalendarEntryOptions.HasFlag(CalendarEntryOptionsEnum.AttendeesToDescription);

            if (!destinationList.Any())
            {
                foreach (var appointment in sourceList)
                {
                    if (appointment.ChildId != null)
                    {
                        var key = AppointmentHelper.GetChildEntryKey(sourceList.CalendarId);
                        if (!appointment.ExtendedProperties.ContainsKey(key))
                        {
                            appointment.ExtendedProperties.Remove(key);
                        }
                        sourceAppointmentsToUpdate.AddCompareForUpdate(appointment);
                    }
                }
                return;
            }

            foreach (var destAppointment in destinationList)
            {
                //If SourceId is null, it is not a copy of any entry from the selected source calendar
                if (destAppointment.SourceId == null)
                {
                    if (syncProfile.SyncSettings.SyncMode == SyncModeEnum.OneWay)
                    {
                        //If mode is one way & user has disabled delete, do not remove this entry, as this is an original entry in the calendar
                        //Else this entry is not a copy of any appointment in source calendar so delete it
                        destOrphanEntries.Add(destAppointment);
                    }
                    else
                    {
                        if (destAppointment.ChildId == null)
                        {
                            var childAppointment = sourceList.FirstOrDefault(t => destAppointment.CompareSourceId(t));
                            if (childAppointment != null)
                            {
                                destAppointment.ChildId = childAppointment.AppointmentId;
                                var key = childAppointment.GetChildEntryKey();
                                if (!destAppointment.ExtendedProperties.ContainsKey(key))
                                {
                                    destAppointment.ExtendedProperties.Add(key, childAppointment.AppointmentId);
                                }
                                else
                                {
                                    destAppointment.ExtendedProperties[key] = childAppointment.AppointmentId;
                                }
                                destAppointmentsToUpdate.AddCompareForUpdate(destAppointment);
                            }
                        }
                        else if (syncProfile.SyncSettings.KeepLastModifiedVersion)
                        {
                            var childAppointment =
                                sourceList.FirstOrDefault(t => t.AppointmentId.Equals(destAppointment.ChildId));
                            if (childAppointment == null)
                            {
                                destAppointmentsToDelete.Add(destAppointment);
                            }
                        }
                    }
                }
                else
                {
                    //If the mode is two way, look for its parent copy in Source calendar
                    Appointment sourceAppointment;
                    if (syncProfile.SyncSettings.SyncMode == SyncModeEnum.TwoWay &&
                        syncProfile.SyncSettings.KeepLastModifiedVersion)
                    {
                        //If no entry was found, it is original entry of the calendar, Ignore
                        //If a child entry is found in source calendar, compare
                        sourceAppointment = sourceList.FirstOrDefault(t => t.CompareSourceId(destAppointment));
                        if (sourceAppointment != null)
                        {
                            //If any entry is found in source appointment and its contents are not equal to source appointment,
                            //If an entry is found and i same, ignore
                            if (!CompareAppointments(destAppointment, sourceAppointment, addDescription,
                                                     addReminders, addAttendeesToDescription))
                            {
                                if (sourceAppointment.LastModified.HasValue && destAppointment.LastModified.HasValue)
                                {
                                    if (destAppointment.LastModified.GetValueOrDefault() >
                                        sourceAppointment.LastModified.GetValueOrDefault())
                                    {
                                        sourceAppointment.CopyDetail(destAppointment,
                                                                     syncProfile.CalendarEntryOptions);
                                        sourceAppointmentsToUpdate.AddCompareForUpdate(sourceAppointment);
                                        continue;
                                    }
                                }
                                //Destination Calendar Entry is not Matching its Source Calendar Entry, Update it
                                destAppointment.CopyDetail(sourceAppointment, syncProfile.CalendarEntryOptions);
                                destAppointmentsToUpdate.AddCompareForUpdate(destAppointment);
                                continue;
                            }
                        }
                    }

                    //If source appointment is not null, means it is a copy of an existing entry in Source calendar
                    sourceAppointment = sourceList.FirstOrDefault(t => t.CompareSourceId(destAppointment));
                    if (sourceAppointment != null)
                    {
                        //If any entry is found in source appointment and its contents are not equal to source appointment
                        if (!CompareAppointments(destAppointment, sourceAppointment, addDescription, addReminders,
                                                 addAttendeesToDescription))
                        {
                            destAppointment.CopyDetail(sourceAppointment, syncProfile.CalendarEntryOptions);
                            destAppointmentsToUpdate.AddCompareForUpdate(destAppointment);
                        }
                    }
                    else
                    {
                        //No parent entry is found, delete it
                        sourceAppointment = sourceList.FirstOrDefault(t =>
                                                                      CompareAppointments(destAppointment, t, addDescription, addReminders,
                                                                                          addAttendeesToDescription));
                        if (sourceAppointment == null)
                        {
                            //If parent entry isn't found
                            destAppointmentsToDelete.Add(destAppointment);
                        }
                    }
                }
            }
        }
        public async Task<CalendarAppointments> GetCalendarEventsInRangeAsync(DateTime startDate, DateTime endDate,
            IDictionary<string, object> calendarSpecificData)
        {
            CheckCalendarSpecificData(calendarSpecificData);


            var service = GetExchangeService(ExchangeServerSettings);

            var calendarview = new CalendarView(startDate, endDate);

            // Get Default Calendar
            var outlookAppointments = new CalendarAppointments();
            var exchangeAppointments = service.FindAppointments(_ewsCalendar.EntryId,
                calendarview);


            service.LoadPropertiesForItems(
                from Item item in exchangeAppointments select item,
                new PropertySet(BasePropertySet.FirstClassProperties) {RequestedBodyType = BodyType.Text});

            if (exchangeAppointments != null)
            {
                foreach (var exchangeAppointment in exchangeAppointments)
                {
                    var appointment = new AppAppointment(exchangeAppointment.Body, exchangeAppointment.Location,
                        exchangeAppointment.Subject, exchangeAppointment.Start, exchangeAppointment.Start)
                    {
                        AppointmentId = exchangeAppointment.Id.UniqueId,
                        AllDayEvent = exchangeAppointment.IsAllDayEvent,
                        OptionalAttendees = GetAttendees(exchangeAppointment.OptionalAttendees),
                        ReminderMinutesBeforeStart = exchangeAppointment.ReminderMinutesBeforeStart,
                        Organizer =
                            new Recipient
                            {
                                Name = exchangeAppointment.Organizer.Name,
                                Email = exchangeAppointment.Organizer.Address
                            },
                        ReminderSet = exchangeAppointment.IsReminderSet,
                        RequiredAttendees = GetAttendees(exchangeAppointment.RequiredAttendees)
                    };
                    outlookAppointments.Add(appointment);
                }
            }
            return outlookAppointments;
        }