private async Task<List<O365Task>> GetO365TasksAsync(GraphServiceClient graphService, Plan plan) { var tasks = await PlanService.GetTasksAsync(plan); var result = new List<O365Task>(); foreach (var item in tasks) { var task = new O365Task { Title = item.title, AssignedTo = !string.IsNullOrEmpty(item.assignedTo) ? (await graphService.Users[item.assignedTo].Request().GetAsync()).DisplayName : "", AssignedBy = !string.IsNullOrEmpty(item.assignedBy) ? (await graphService.Users[item.assignedBy].Request().GetAsync()).DisplayName : "", AssignedDate = item.assignedDateTime.HasValue ? item.assignedDateTime.Value.DateTime.ToLocalTime().ToString() : "" }; result.Add(task); } return result; }
public static async Task<task[]> GetTasksAsync(Plan plan) { var accessToken = AuthenticationHelper.GetGraphAccessTokenAsync(); List<task> tasks = new List<task>(); var pageEndPoint = string.Format("{0}plans/{1}/Tasks?$filter=percentComplete+ne+100", AADAppSettings.GraphBetaResourceUrl, plan.id); using (var client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await accessToken); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var responseMessage = await client.GetAsync(pageEndPoint); if (responseMessage.StatusCode != System.Net.HttpStatusCode.OK) throw new Exception(); var payload = await responseMessage.Content.ReadAsStringAsync(); var jobject = JObject.Parse(payload); foreach (var item in jobject["value"].Children()) { tasks.Add(new task { title = item["title"].ToString(), assignedTo = !string.IsNullOrEmpty(item["assignedTo"].ToString()) ? item["assignedTo"].ToString() : "", assignedBy = !string.IsNullOrEmpty(item["assignedBy"].ToString()) ? item["assignedBy"].ToString() : "", assignedDateTime = !string.IsNullOrEmpty(item["assignedDateTime"].ToString()) ? DateTimeOffset.Parse(item["assignedDateTime"].ToString()) : (DateTimeOffset?)null, percentComplete = !string.IsNullOrEmpty(item["percentComplete"].ToString()) ? Convert.ToInt32(item["percentComplete"].ToString()) : 0 }); } } return tasks.Where(i => i.percentComplete.HasValue && i.percentComplete.Value < 100).ToArray(); }