コード例 #1
0
        public async void UpdateGoogleEvent(Database.Entities.Event ev, List <User> attendees)
        {
            try
            {
                var googleEv = await GetGoogleEvent(ev.GoogleEventId);

                googleEv.Summary        = ev.Title;
                googleEv.Description    = ev.Description;
                googleEv.Location       = ev.Location;
                googleEv.Start.DateTime = ev.StartDate;
                googleEv.End.DateTime   = ev.EndDate;

                if (attendees.Any())
                {
                    foreach (var attendee in attendees)
                    {
                        googleEv.Attendees.Add(new EventAttendee
                        {
                            DisplayName = attendee.Name,
                            Email       = attendee.Email
                        });
                    }
                }

                var updateRequest = _calendarService.Events.Update(googleEv, calendarId, ev.GoogleEventId);
                updateRequest.SendUpdates = 0;
                await updateRequest.ExecuteAsync();
            }
            catch (Exception e)
            {
                e.Message.ToString();
            }
        }
コード例 #2
0
        public async Task <string> CreateGoogleEvent(Database.Entities.Event ev, List <User> attendees)
        {
            try
            {
                //Create google event
                var googleEv = new Google.Apis.Calendar.v3.Data.Event()
                {
                    Summary     = ev.Title,
                    Description = ev.Description,
                    Location    = ev.Location,
                    Start       = new EventDateTime
                    {
                        DateTime = ev.StartDate,
                        TimeZone = "Europe/Stockholm"
                    },
                    End = new EventDateTime
                    {
                        DateTime = ev.EndDate,
                        TimeZone = "Europe/Stockholm"
                    },
                    Created = ev.CreateDate
                };

                //Get the creator details of the event
                var organizer = attendees.Find(u => u.Id == ev.CreatorId);

                //Inserting many attendees into the event causes calendar usage exceed,
                //comment out the attendee lines if internal server error 500
                if (attendees.Any())
                {
                    googleEv.Attendees = attendees.Select(u =>
                                                          new EventAttendee
                    {
                        DisplayName    = u.Name,
                        Email          = u.Email,
                        ResponseStatus = (u.Email == organizer.Email) ? "accepted" : null
                    }).ToList();
                }

                //Insert in primary(default) calendar for account and send email notification to all attendees
                var insertRequest = _calendarService.Events.Insert(googleEv, calendarId);
                insertRequest.SendUpdates = 0;
                var createdGoogleEv = await insertRequest.ExecuteAsync();

                var googleEventId = createdGoogleEv.Id;

                return(googleEventId);
            }
            catch (Exception e)
            {
                e.Message.ToString();
            }

            return(null);
        }