/// <summary> /// Adds the specified Calendar to the collection via POST. /// </summary> /// <param name="calendar">The Calendar to add.</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/> for the request.</param> /// <returns>The created Calendar.</returns> public System.Threading.Tasks.Task <Calendar> AddAsync(Calendar calendar, CancellationToken cancellationToken) { this.ContentType = "application/json"; this.Method = "POST"; return(this.SendAsync <Calendar>(calendar, cancellationToken)); }
public async Task <string> CreateCalendarEvent(HttpContextBase httpContextBase, string name, List <Exam> exams) { string token = await GetAccessToken(httpContextBase); if (string.IsNullOrEmpty(token)) { return(null); } GraphServiceClient client = new GraphServiceClient( new DelegateAuthenticationProvider( (requestMessage) => { requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token); return(Task.FromResult(0)); })); DBManager db = new DBManager(); if (db.GetUserGenCalendars(user).ToUpper() == "TRUE") { try { Debug.WriteLine("Create Calendar ... "); Microsoft.Graph.Calendar examCalendar = new Microsoft.Graph.Calendar { Name = name, Events = new CalendarEventsCollectionPage(), }; Debug.WriteLine("Check Calendars ... "); var calendars = await client.Me.Calendars.Request().GetAsync(); bool exists = false; foreach (Microsoft.Graph.Calendar calendar in calendars) { if (calendar.Name == name) { Debug.WriteLine("Delete Calendar ... "); exists = true; await client.Me.Calendars[calendar.Id].Request().DeleteAsync(); } } if (!exists) { Debug.WriteLine("Add Calendar ... "); await client.Me.Calendars.Request().AddAsync(examCalendar); } foreach (Exam exam in exams) { DateTime examTime = Convert.ToDateTime(exam.Start); DateTime start = new DateTime( DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, examTime.Hour, examTime.Minute, examTime.Second, examTime.Millisecond, DateTimeKind.Utc); foreach (System.DayOfWeek weekday in Enum.GetValues(typeof(System.DayOfWeek))) { if (exam.Day.ToUpper() == weekday.ToString().ToUpper()) { start = start.AddDays((weekday + 1) - (DateTime.Now.DayOfWeek + 1)); break; } } TimeSpan span = Convert.ToDateTime(exam.End).Subtract(Convert.ToDateTime(exam.Start)); DateTime end = start.AddHours(span.Hours); Event entry = new Event { Calendar = examCalendar, Start = new DateTimeTimeZone { DateTime = string.Format("{0:s}", start), TimeZone = TimeZone.CurrentTimeZone.StandardName }, End = new DateTimeTimeZone { DateTime = string.Format("{0:s}", end), TimeZone = TimeZone.CurrentTimeZone.StandardName }, Body = new ItemBody { Content = exam.Room + "\n Faculty: " + exam.Faculty + "\n Proctor: " + exam.Proctor }, Subject = exam.Code + ": " + exam.Name + ", Section " + exam.Section, SingleValueExtendedProperties = new EventSingleValueExtendedPropertiesCollectionPage { new SingleValueLegacyExtendedProperty { Id = "String " + Guid.NewGuid().ToString() + " Name TruckleSoft1", Value = "CLM_MidweekMeeting" } } }; Debug.WriteLine("New Event ... "); //await client.Me.Calendars[examCalendar.Id].Events.Request().AddAsync(entry); await client.Me.Events.Request().AddAsync(entry); } //Debug.WriteLine(JsonConvert.SerializeObject(calendars)); //await client.Me.Calendars["Test"].Events.Request().AddAsync(entry); //await client.Me.Events.Request().AddAsync(entry); } catch (Exception e) { Debug.WriteLine(e.ToString()); Debug.WriteLine(e.InnerException); } } return(""); }
/// <summary> /// Adds the specified Calendar to the collection via POST. /// </summary> /// <param name="calendar">The Calendar to add.</param> /// <returns>The created Calendar.</returns> public System.Threading.Tasks.Task <Calendar> AddAsync(Calendar calendar) { return(this.AddAsync(calendar, CancellationToken.None)); }