Example #1
0
        public static bool AddUpdateDeleteEvent(List <GoogleCalendarAppointmentModel> GoogleCalendarAppointmentModelList, List <GoogleTokenModel> GoogleTokenModelList, double TimeOffset)
        {
            //Get the calendar service for a user to add/update/delete events
            CalendarService calService = GetCalendarService(GoogleTokenModelList[0]);

            if (GoogleCalendarAppointmentModelList != null && GoogleCalendarAppointmentModelList.Count > 0)
            {
                foreach (GoogleCalendarAppointmentModel GoogleCalendarAppointmentModelObj in GoogleCalendarAppointmentModelList)
                {
                    EventsResource er     = new EventsResource(calService);
                    string         ExpKey = "EventID";
                    string         ExpVal = GoogleCalendarAppointmentModelObj.EventID;

                    var queryEvent = er.List(calID);
                    queryEvent.SharedExtendedProperty = ExpKey + "=" + ExpVal; //"EventID=9999"
                    var EventsList = queryEvent.Execute();

                    //to restrict the appointment for specific staff only
                    //Delete this appointment from google calendar
                    if (GoogleCalendarAppointmentModelObj.DeleteAppointment == true)
                    {
                        string FoundEventID = String.Empty;
                        foreach (Event evItem in EventsList.Items)
                        {
                            FoundEventID = evItem.Id;
                            if (!String.IsNullOrEmpty(FoundEventID))
                            {
                                er.Delete(calID, FoundEventID).Execute();
                            }
                        }
                        return(true);
                    }
                    //Add if not found OR update if appointment already present on google calendar
                    else
                    {
                        Event eventEntry = new Event();

                        EventDateTime StartDate = new EventDateTime();
                        EventDateTime EndDate   = new EventDateTime();
                        StartDate.Date = GoogleCalendarAppointmentModelObj.EventStartTime.ToString("yyyy-MM-dd"); //"2014-11-17";
                        EndDate.Date   = StartDate.Date;                                                          //GoogleCalendarAppointmentModelObj.EventEndTime

                        //Always append Extended Property whether creating or updating event
                        Event.ExtendedPropertiesData exp = new Event.ExtendedPropertiesData();
                        exp.Shared = new Dictionary <string, string>();
                        exp.Shared.Add(ExpKey, ExpVal);

                        eventEntry.Summary            = GoogleCalendarAppointmentModelObj.EventTitle;
                        eventEntry.Start              = StartDate;
                        eventEntry.End                = EndDate;
                        eventEntry.Location           = GoogleCalendarAppointmentModelObj.EventLocation;
                        eventEntry.Description        = GoogleCalendarAppointmentModelObj.EventDetails;
                        eventEntry.ExtendedProperties = exp;

                        string FoundEventID = String.Empty;
                        foreach (var evItem in EventsList.Items)
                        {
                            FoundEventID = evItem.Id;
                            if (!String.IsNullOrEmpty(FoundEventID))
                            {
                                //Update the event
                                er.Update(eventEntry, calID, FoundEventID).Execute();
                            }
                        }

                        if (String.IsNullOrEmpty(FoundEventID))
                        {
                            //create the event
                            er.Insert(eventEntry, calID).Execute();
                        }

                        return(true);
                    }
                }
            }

            return(false);
        }