예제 #1
0
        private Event CreateGoogleCalendarEvent(Appointment calendarAppointment, bool addDescription, bool addReminder,
            bool addAttendees, bool attendeesToDescription)
        {
            //Create Event
            var googleEvent = new Event
            {
                Start = new EventDateTime(),
                End = new EventDateTime(),
                Summary = calendarAppointment.Subject,
                Description = calendarAppointment.GetDescriptionData(addDescription, attendeesToDescription),
                Location = calendarAppointment.Location,
                Visibility = calendarAppointment.Privacy,
                Transparency = (calendarAppointment.BusyStatus == BusyStatusEnum.Free) ? "transparent" : "opaque",
                //Need to make recurring appointment IDs unique - append the item's date   
                ExtendedProperties =
                    new Event.ExtendedPropertiesData
                    {
                        Private__ = 
                            new Dictionary<string, string>
                            {
                                {calendarAppointment.GetSourceEntryKey(), calendarAppointment.AppointmentId}
                            }
                    }
            };

            if (EventCategory != null && !string.IsNullOrEmpty(EventCategory.ColorNumber))
            {
                googleEvent.ColorId = EventCategory.ColorNumber;
            }
            //Add Start/End Time
            if (calendarAppointment.AllDayEvent)
            {
                if (calendarAppointment.StartTime.HasValue)
                {
                    googleEvent.Start.Date = calendarAppointment.StartTime.Value.ToString("yyyy-MM-dd");
                }
                if (calendarAppointment.EndTime.HasValue)
                {
                    googleEvent.End.Date = calendarAppointment.EndTime.Value.ToString("yyyy-MM-dd");
                }
            }
            else
            {
                googleEvent.Start.DateTimeRaw = calendarAppointment.Rfc339FormatStartTime;
                googleEvent.End.DateTimeRaw = calendarAppointment.Rfc339FormatEndTime;
            }

            //Add Reminder
            if (addReminder && calendarAppointment.ReminderSet)
            {
                googleEvent.Reminders = new Event.RemindersData
                {
                    UseDefault = false,
                    Overrides = new List<EventReminder>
                    {
                        new EventReminder
                        {
                            Method = "popup",
                            Minutes = calendarAppointment.ReminderMinutesBeforeStart
                        }
                    }
                };
            }

            if (googleEvent.Attendees == null)
            {
                googleEvent.Attendees = new List<EventAttendee>();
            }

            if (addAttendees && !attendeesToDescription)
            {
                //Add Required Attendees
                AddEventAttendees(calendarAppointment.RequiredAttendees, googleEvent, false);

                //Add optional Attendees
                AddEventAttendees(calendarAppointment.OptionalAttendees, googleEvent, true);
            }
            //Add Organizer
            if (calendarAppointment.Organizer != null && calendarAppointment.Organizer.Email.IsValidEmailAddress())
            {
                googleEvent.Organizer = new Event.OrganizerData
                {
                    DisplayName = calendarAppointment.Organizer.Name,
                    Email = calendarAppointment.Organizer.Email
                };
            }

            return googleEvent;
        }
        /// <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;
        }