private bool UpdateAppointment(bool addDescription, bool addReminder, bool addAttendees, bool attendeesToDescription, AppointmentItem appItem, Appointment calendarAppointment) { Recipients recipients = null; UserProperties userProperties = null; try { appItem.Subject = calendarAppointment.Subject; appItem.Location = calendarAppointment.Location; appItem.BusyStatus = calendarAppointment.GetOutlookBusyStatus(); if (EventCategory != null) { appItem.Categories = EventCategory.CategoryName; } if (calendarAppointment.AllDayEvent != appItem.AllDayEvent) { appItem.AllDayEvent = calendarAppointment.AllDayEvent; } appItem.Sensitivity = calendarAppointment.GetAppointmentSensitivity(); appItem.Start = calendarAppointment.StartTime.GetValueOrDefault(); appItem.End = calendarAppointment.EndTime.GetValueOrDefault(); if (addDescription) { appItem.Body = calendarAppointment.Description; } if (addAttendees && !attendeesToDescription) { recipients = appItem.Recipients; if (calendarAppointment.RequiredAttendees != null) { calendarAppointment.RequiredAttendees.ForEach(rcptName => { if (!CheckIfRecipientExists(recipients, rcptName)) { var recipient = appItem.Recipients.Add($"{rcptName.Name}<{rcptName.Email}>"); recipient.Type = (int) OlMeetingRecipientType.olRequired; recipient.Resolve(); } }); } if (calendarAppointment.OptionalAttendees != null) { calendarAppointment.OptionalAttendees.ForEach(rcptName => { if (!CheckIfRecipientExists(recipients, rcptName)) { var recipient = appItem.Recipients.Add($"{rcptName.Name}<{rcptName.Email}>"); recipient.Type = (int) OlMeetingRecipientType.olOptional; recipient.Resolve(); } }); } } if (addReminder) { if (appItem.ReminderSet != calendarAppointment.ReminderSet) { appItem.ReminderMinutesBeforeStart = calendarAppointment.ReminderMinutesBeforeStart; if (calendarAppointment.ReminderSet && appItem.ReminderMinutesBeforeStart != calendarAppointment.ReminderMinutesBeforeStart) { appItem.ReminderMinutesBeforeStart = calendarAppointment.ReminderMinutesBeforeStart; } } } userProperties = appItem.UserProperties; if (userProperties != null) { for (var i = 0; i < userProperties.Count; i++) { userProperties.Remove(i + 1); } foreach (var extendedProperty in calendarAppointment.ExtendedProperties) { var sourceProperty = userProperties.Add(extendedProperty.Key, OlUserPropertyType.olText); sourceProperty.Value = extendedProperty.Value; } } appItem.Save(); } catch (Exception exception) { Logger.Error(exception); return false; } finally { if (recipients != null) { Marshal.FinalReleaseComObject(recipients); } if (userProperties != null) { Marshal.FinalReleaseComObject(userProperties); } if (appItem != null) { Marshal.FinalReleaseComObject(appItem); } } return true; }
/// <summary> /// </summary> /// <param name="addDescription"></param> /// <param name="addReminder"></param> /// <param name="addAttendees"></param> /// <param name="attendeesToDescription"></param> /// <param name="appItem"></param> /// <param name="calendarAppointment"></param> private Appointment AddAppointment(bool addDescription, bool addReminder, bool addAttendees, bool attendeesToDescription, AppointmentItem appItem, Appointment calendarAppointment, string id) { Recipients recipients = null; UserProperties userProperties = null; Appointment createdAppointment = null; try { appItem.Subject = calendarAppointment.Subject; if (!calendarAppointment.RequiredAttendees.Any() && !calendarAppointment.OptionalAttendees.Any() && AddAsAppointments) { appItem.MeetingStatus = OlMeetingStatus.olNonMeeting; } else { appItem.MeetingStatus = OlMeetingStatus.olMeeting; } appItem.Sensitivity = calendarAppointment.GetAppointmentSensitivity(); appItem.Location = calendarAppointment.Location; appItem.BusyStatus = calendarAppointment.GetOutlookBusyStatus(); recipients = appItem.Recipients; if (EventCategory != null) { appItem.Categories = EventCategory.CategoryName; } if (calendarAppointment.AllDayEvent) { appItem.AllDayEvent = true; } appItem.Start = calendarAppointment.StartTime.GetValueOrDefault(); appItem.End = calendarAppointment.EndTime.GetValueOrDefault(); appItem.Body = calendarAppointment.GetDescriptionData(addDescription, attendeesToDescription); Recipient organizer = null; if (addAttendees && !attendeesToDescription) { calendarAppointment.RequiredAttendees?.ForEach(rcptName => { var recipient = appItem.Recipients.Add($"{rcptName.Name}<{rcptName.Email}>"); if (SetOrganizer && calendarAppointment.Organizer != null && rcptName.Name.Equals(calendarAppointment.Organizer.Name)) { recipient.Type = (int) OlMeetingRecipientType.olOrganizer; recipient.Resolve(); organizer = recipient; } else { recipient.Type = (int) OlMeetingRecipientType.olRequired; recipient.Resolve(); } }); calendarAppointment.OptionalAttendees?.ForEach(rcptName => { var recipient = appItem.Recipients.Add($"{rcptName.Name}<{rcptName.Email}>"); if (SetOrganizer && calendarAppointment.Organizer != null && rcptName.Name.Equals(calendarAppointment.Organizer.Name)) { recipient.Type = (int) OlMeetingRecipientType.olOrganizer; recipient.Resolve(); organizer = recipient; } else { recipient.Type = (int)OlMeetingRecipientType.olOptional; recipient.Resolve(); } }); } else if (SetOrganizer && calendarAppointment.Organizer != null) { var recipient = appItem.Recipients.Add( $"{calendarAppointment.Organizer.Name}<{calendarAppointment.Organizer.Email}>"); recipient.Type = (int)OlMeetingRecipientType.olOrganizer; recipient.Resolve(); organizer = recipient; } SetAppointmentOrganizer(appItem, organizer); if (addReminder) { appItem.ReminderMinutesBeforeStart = calendarAppointment.ReminderMinutesBeforeStart; appItem.ReminderSet = calendarAppointment.ReminderSet; } userProperties = appItem.UserProperties; if (userProperties != null) { var sourceProperty = userProperties.Add(calendarAppointment.GetSourceEntryKey(), OlUserPropertyType.olText); sourceProperty.Value = calendarAppointment.AppointmentId; } appItem.Save(); createdAppointment = GetAppointmentFromItem(id, appItem); } catch (Exception exception) { Logger.Error(exception); } finally { if (userProperties != null) { Marshal.FinalReleaseComObject(userProperties); } if (recipients != null) { Marshal.FinalReleaseComObject(recipients); } if (appItem != null) { Marshal.FinalReleaseComObject(appItem); } } return createdAppointment; }