internal async Task<List<EventViewModel>> GetCalendarEvents() { var eventsResults = new List<EventViewModel>(); try { var restURL = string.Format("{0}/me/calendar/events?$top=10&$filter=End/DateTime ge '{1}'", AuthenticationHelper.ResourceBetaUrl, DateTime.Now.ToString("yyyy/MM/dd HH:mm")); string responseString = await AuthenticationHelper.GetJsonAsync(restURL); if (responseString != null) { var jsonresult = JObject.Parse(responseString); foreach (var item in jsonresult["value"]) { EventViewModel calendarEventModel = new EventViewModel(); calendarEventModel.Subject = !string.IsNullOrEmpty(item["subject"].ToString()) ? item["subject"].ToString() : string.Empty; calendarEventModel.Start = !string.IsNullOrEmpty(item["start"]["dateTime"].ToString()) ? DateTime.Parse(item["start"]["dateTime"].ToString()) : new DateTime(); calendarEventModel.End = !string.IsNullOrEmpty(item["end"]["dateTime"].ToString()) ? DateTime.Parse(item["end"]["dateTime"].ToString()) : new DateTime(); calendarEventModel.Id = !string.IsNullOrEmpty(item["id"].ToString()) ? item["id"].ToString() : string.Empty; calendarEventModel.LocationName = !string.IsNullOrEmpty(item["location"]["displayName"].ToString()) ? item["location"]["displayName"].ToString() : string.Empty; calendarEventModel.StartTime = calendarEventModel.Start.ToLocalTime().TimeOfDay; calendarEventModel.EndTime = calendarEventModel.End.ToLocalTime().TimeOfDay; string bodyType = !string.IsNullOrEmpty(item["body"]["contentType"].ToString()) ? item["body"]["contentType"].ToString() : string.Empty; string bodyContent = !string.IsNullOrEmpty(item["body"]["content"].ToString()) ? item["body"]["content"].ToString() : string.Empty; if (bodyType == "html") { bodyContent = Regex.Replace(bodyContent, "<[^>]*>", ""); bodyContent = Regex.Replace(bodyContent, "\n", ""); bodyContent = Regex.Replace(bodyContent, "\r", ""); } calendarEventModel.BodyContent = bodyContent; calendarEventModel.Attendees = !string.IsNullOrEmpty(item["attendees"].ToString()) ? BuildAttendeeList(item["attendees"].ToString()) : string.Empty; calendarEventModel.UpdateDisplayString(); eventsResults.Add(calendarEventModel); } } } catch (Exception el) { el.ToString(); } return eventsResults.OrderBy(e => e.Start).ToList(); }
internal async Task<List<EventViewModel>> GetCalendarEvents() { // Make sure we have a reference to the calendar client var calendarClient = await AuthenticationHelper.EnsureOutlookClientCreatedAsync(); List<EventViewModel> returnResults = new List<EventViewModel>(); // Obtain calendar event data var eventsResults = await (from i in calendarClient.Me.Events where i.End >= DateTimeOffset.UtcNow select i).Take(10).ExecuteAsync(); var events = eventsResults.CurrentPage.OrderBy(e => e.Start); foreach (IEvent calendarEvent in events) { IEvent thisEvent = await GetEventDetailsAsync(calendarEvent.Id); EventViewModel calendarEventModel = new EventViewModel(thisEvent); returnResults.Add(calendarEventModel); } return returnResults; }
internal async Task<List<EventViewModel>> GetCalendarEvents() { var eventsResults = new List<EventViewModel>(); try { var graphClient = await AuthenticationHelper.GetGraphServiceClientAsync(); var eventsPage = await graphClient.Me.Calendar.Events.Request().Top(10).Filter(string.Format("End/DateTime ge '{0}'", DateTime.Now.ToString("yyyy/MM/dd HH:mm"))).GetAsync(); var events = eventsPage.CurrentPage; foreach (var item in events) { EventViewModel calendarEventModel = new EventViewModel(); calendarEventModel.Subject = item.Subject; calendarEventModel.Start = DateTime.Parse(item.Start.DateTime); calendarEventModel.End = DateTime.Parse(item.End.DateTime); calendarEventModel.Id = item.Id; calendarEventModel.LocationName = item.Location.DisplayName; calendarEventModel.StartTime = calendarEventModel.Start.ToLocalTime().TimeOfDay; calendarEventModel.EndTime = calendarEventModel.End.ToLocalTime().TimeOfDay; string bodyType = item.Body.ContentType == BodyType.Html ? "html" : "text"; string bodyContent = item.Body.Content; if (item.Body.ContentType == BodyType.Html) { bodyContent = Regex.Replace(bodyContent, "<[^>]*>", ""); bodyContent = Regex.Replace(bodyContent, "\n", ""); bodyContent = Regex.Replace(bodyContent, "\r", ""); } calendarEventModel.BodyContent = bodyContent; calendarEventModel.Attendees = BuildAttendeeList(item.Attendees); calendarEventModel.UpdateDisplayString(); eventsResults.Add(calendarEventModel); } } catch (Exception el) { el.ToString(); } return eventsResults.OrderBy(e => e.Start).ToList(); }
/// <summary> /// Gets a collection of events for today's calendar. /// </summary> /// <param name="hoursBefore">int. The beginning of the TimeSpan that defines which events are returned.</param> /// <param name="hoursAfter">int. The end of the TimeSpan that defines which events are returned.</param> /// <returns>A collection of all calendar events found for the specified time range.</returns> internal async Task<List<EventViewModel>> GetTodaysCalendar(int hoursBefore, int hoursAfter) { // Make sure we have a reference to the calendar client var calendarClient = await AuthenticationHelper.EnsureOutlookClientCreatedAsync(); List<EventViewModel> returnResults = new List<EventViewModel>(); // Obtain calendar event data for start times from the range of 6 hours // before now to 6 hours after now. Get the first 48 calender events in the range. // This results in a call to the service. var eventsResults = await (from i in calendarClient.Me.Calendar.Events where i.Start >= DateTimeOffset.Now.Subtract(new TimeSpan(hoursBefore, 0, 0)) && i.Start <= DateTimeOffset.Now.AddHours(hoursAfter) select i).Take(48).ExecuteAsync(); var events = eventsResults.CurrentPage.OrderBy(e => e.Start); foreach (IEvent calendarEvent in events) { IEvent thisEvent = await GetEventDetailsAsync(calendarEvent.Id); EventViewModel calendarEventModel = new EventViewModel(thisEvent); returnResults.Add(calendarEventModel); } return returnResults; }