public void UpdateEvent(int UserId, int LocalId, TimeBlockUpdateRequest updated) { string EventId = exCalHelperService.GetExternalEventId(LocalId, 1); // provider 1 = google string CalendarApi = "https://www.googleapis.com/calendar/v3/calendars/primary/events/" + EventId + "?sendNotifications=true&access_token="; string AccessToken = null; if (EventId != null) { AccessToken = oAuthService.GetGoogleAccessToken(UserId); } string gapiRespObject; if (EventId != null && AccessToken != null) { string gStartTime = updated.StartDate + "T" + updated.StartTime + ":00"; string gEndTime = updated.EndDate + "T" + updated.EndTime + ":00"; HttpWebRequest updateRequest = (HttpWebRequest)WebRequest.Create(CalendarApi + AccessToken); updateRequest.Method = "PUT"; updateRequest.ContentType = "application/json"; // set bg color int categoryId = 2; //categoryId = updated.ActivityCategory.Value; if (updated.ActivityCategory.HasValue) { categoryId = updated.ActivityCategory.Value; } string bgColor = exCalHelperService.GetGoogleColor(categoryId); // timezone failsafe logic string userTimezone = "America/Los_Angeles"; if (updated.TimeZone != null) { userTimezone = updated.TimeZone; } // for events marked as canceled string eventStatus = "confirmed"; if (updated.Canceled) { eventStatus = "cancelled"; } // placeholders for the eventual start & end fields object startField; object endField; // regular events var startDateTime = new { dateTime = gStartTime, timeZone = userTimezone }; var endDateTime = new { dateTime = gEndTime, timeZone = userTimezone }; // all day events var startDate = new { date = updated.StartDate }; var endDate = new { date = updated.EndDate }; // select one if (updated.AllDay) { startField = startDate; endField = endDate; } else { startField = startDateTime; endField = endDateTime; } List <string> validEmails = exCalHelperService.ReturnValidEmails(updated.GuestEmail); string json = ""; if (validEmails.Count != 0) { List <GoogleAttendees> attendeeEmails = AttendeeList(validEmails); var tempObj = new { summary = updated.Title, location = updated.Location, description = updated.Description, start = startField, end = endField, colorId = bgColor, reminders = new { useDefault = true }, attendees = attendeeEmails, status = eventStatus }; json = JsonConvert.SerializeObject(tempObj); } else { json = new JavaScriptSerializer().Serialize(new { summary = updated.Title, location = updated.Location, description = updated.Description, start = startField, end = endField, colorId = bgColor, status = eventStatus }); } try { using (var streamWriter = new StreamWriter(updateRequest.GetRequestStream())) { streamWriter.Write(json); } System.Diagnostics.Debug.WriteLine(json); var httpResponse = (HttpWebResponse)updateRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { gapiRespObject = streamReader.ReadToEnd(); } var GoogleResp = (JObject)JsonConvert.DeserializeObject(gapiRespObject); string ETag = GoogleResp["etag"].Value <string>(); exCalHelperService.UpdateETag(EventId, ETag); } catch (WebException ex) { string resp; using (var streamReader = new StreamReader(ex.Response.GetResponseStream())) { resp = streamReader.ReadToEnd(); } System.Diagnostics.Debug.WriteLine("Failed to UPDATE"); System.Diagnostics.Debug.WriteLine(resp); } } }
public void UpdateEvent(int UserId, int LocalId, TimeBlockUpdateRequest updated) { string EventId = exCalHelperService.GetExternalEventId(LocalId, 2); // provider 2 = microsoft string CalendarApi = "https://graph.microsoft.com/v1.0/me/calendar/events/" + EventId; string AccessToken = null; if (EventId != null) { AccessToken = oAuthService.GetMicrosoftAccessToken(UserId); } string respObject; if (EventId != null && AccessToken != null) { HttpWebRequest updateRequest = (HttpWebRequest)WebRequest.Create(CalendarApi); updateRequest.Method = "PATCH"; updateRequest.ContentType = "application/json"; updateRequest.Headers["authorization"] = AccessToken; string startTime = updated.StartDate + "T" + updated.StartTime + ":00"; string endTime = updated.EndDate + "T" + updated.EndTime + ":00"; // if all day, times need to be set at midnight for Microsoft if (updated.AllDay) { startTime = updated.StartDate + "T00:00:00"; if (updated.StartDate == updated.EndDate) { // a single all day event needs to be >24 hours otherwise MS will reject string nextDay = DateTime.Parse(updated.EndDate).AddDays(1).ToString("yyyy-MM-dd"); endTime = nextDay + "T00:00:00"; } else { endTime = updated.EndDate + "T00:00:00"; } } // grab activity category and turn it into a color int categoryId = 2; // failsafe = green if (updated.ActivityCategory.HasValue) { categoryId = updated.ActivityCategory.Value; } string[] activityColor = new[] { exCalHelperService.GetMicrosoftColor(categoryId) }; // timezone failsafe logic string userTimezone = "America/Los_Angeles"; if (updated.TimeZone != null) { userTimezone = updated.TimeZone; } List <string> validEmails = exCalHelperService.ReturnValidEmails(updated.GuestEmail); if (updated.Canceled && validEmails.Count != 0) // no need to trigger cancel API for guest-less events { CancelEvent(EventId, AccessToken); } else { string json = ""; if (validEmails.Count != 0) { List <MicrosoftAttendees.Container> attendeeEmails = AttendeeList(validEmails); var tempObj = new { subject = updated.Title, body = new { contentType = "Text", content = updated.Description }, location = new { displayName = updated.Location }, start = new { dateTime = startTime, timeZone = userTimezone }, end = new { dateTime = endTime, timeZone = userTimezone }, isAllDay = updated.AllDay, categories = activityColor, attendees = attendeeEmails }; json = JsonConvert.SerializeObject(tempObj); } else { json = new JavaScriptSerializer().Serialize(new { subject = updated.Title, body = new { contentType = "Text", content = updated.Description }, location = new { displayName = updated.Location }, start = new { dateTime = startTime, timeZone = userTimezone }, end = new { dateTime = endTime, timeZone = userTimezone }, isAllDay = updated.AllDay, categories = activityColor }); } try { using (var streamWriter = new StreamWriter(updateRequest.GetRequestStream())) { streamWriter.Write(json); } System.Diagnostics.Debug.WriteLine(json); var httpResponse = (HttpWebResponse)updateRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { respObject = streamReader.ReadToEnd(); } var serverResp = (JObject)JsonConvert.DeserializeObject(respObject); string ETag = serverResp["@odata.etag"].Value <string>(); exCalHelperService.UpdateETag(EventId, ETag); } catch (WebException ex) { string resp; using (var streamReader = new StreamReader(ex.Response.GetResponseStream())) { resp = streamReader.ReadToEnd(); } System.Diagnostics.Debug.WriteLine("Failed to UPDATE"); System.Diagnostics.Debug.WriteLine(resp); } } } }