Ejemplo n.º 1
0
        private static async Task DeletePlanTasksAsync(int metricId)
        {
            var filter = string.Format(
                @"<Eq>
                    <FieldRef Name='{0}' LookupId='TRUE'/>
                    <Value Type='Lookup'>{1}</Value> 
                </Eq>",
                SPLists.MetricIdeas.Columns.Metric, metricId);

            var query = new CamlQuery();

            query.ViewXml = string.Format("<View><Query><Where>{0}</Where></Query></View>", filter);

            using (var clientContext = await(AuthenticationHelper.GetSharePointClientContextAsync(Permissions.Application)))
            {
                var items = clientContext.GetItems(SPLists.MetricIdeas.Title, query);
                var graphServiceClient = await AuthenticationHelper.GetGraphServiceClientAsync();

                for (int i = items.Count - 1; i > -1; i--)
                {
                    var taskId = items[i].GetFieldValueStr(SPLists.MetricIdeas.Columns.TaskId);

                    PlannerTask task = null;
                    try
                    {
                        task = await graphServiceClient.Planner.Tasks[taskId].Request().GetAsync();
                    }
                    catch (ServiceException)
                    {
                    }

                    if (task != null)
                    {
                        await graphServiceClient.Planner.Tasks[taskId]
                        .Request(new[] { new HeaderOption("If-Match", task.GetEtag()) })
                        .DeleteAsync();
                        items[i].DeleteObject();
                        clientContext.ExecuteQuery();
                    }
                }
            }
        }
        public PlannerTaskViewModel(PlannerTask task)
        {
            Id            = task.Id;
            Title         = task.Title;
            BucketId      = task.BucketId;
            DueDateTime   = task.DueDateTime;
            AssignmentIds = task.Assignments?.Assignees.ToList();
            if (AssignmentIds == null)
            {
                AssignmentIds = new List <string>();
            }

            Assignments = new ObservableCollection <Person>(AssignmentIds.Select(s => new Person()
            {
                Id = s
            }));
            Assignments.CollectionChanged += Assignments_CollectionChanged;
            PercentComplete = task.PercentComplete ?? 0;
            CreatedDateTime = task.CreatedDateTime;
            ETag            = task.GetEtag();
        }
        private async Task DeleteTaskAsync(PlannerTaskViewModel task)
        {
            MessageDialog confirmDialog = new MessageDialog(DeleteConfirmDialogMessage);

            confirmDialog.Commands.Add(new UICommand {
                Id = DeleteConfirmDialogYes, Label = DeleteConfirmDialogYesLabel
            });
            confirmDialog.Commands.Add(new UICommand {
                Id = DeleteConfirmDialogNo, Label = DeleteConfirmDialogNoLabel
            });
            confirmDialog.DefaultCommandIndex = 0;
            confirmDialog.CancelCommandIndex  = 1;
            IUICommand result = await confirmDialog.ShowAsync();

            if (result.Id.ToString() == DeleteConfirmDialogYes)
            {
                try
                {
                    GraphServiceClient graphClient = await GraphServiceHelper.GetGraphServiceClientAsync();

                    if (graphClient != null)
                    {
                        PlannerTask taskToUpdate = await graphClient.Planner.Tasks[task.Id].Request().GetAsync();
                        await graphClient.Planner.Tasks[task.Id].Request().Header(HttpHeaderIfMatch, taskToUpdate.GetEtag()).DeleteAsync();
                        task.PropertyChanged -= TaskViewModel_PropertyChanged;
                        Tasks.Remove(task);
                        _allTasks.Remove(task);
                    }
                }
                catch (Exception exception)
                {
                    MessageDialog messageDialog = new MessageDialog(exception.Message);
                    await messageDialog.ShowAsync();
                }
            }
        }
        private async void TaskViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            PlannerTaskViewModel plannerTaskViewModel = sender as PlannerTaskViewModel;

            if (Tasks.Contains(plannerTaskViewModel))
            {
                Dictionary <string, object> task = new Dictionary <string, object>();
                bool skipUpdate = false;
                switch (e.PropertyName)
                {
                case nameof(plannerTaskViewModel.AssignmentIds):
                    await GetAssignmentsAsync(plannerTaskViewModel);

                    PlannerAssignments assignments = new PlannerAssignments();

                    foreach (string assignee in plannerTaskViewModel.AssignmentIds)
                    {
                        assignments.AddAssignee(assignee);
                    }

                    task.Add(TaskAssignmentsJsonName, assignments);
                    break;

                case nameof(plannerTaskViewModel.Title):
                    task.Add(TaskTitleJsonName, plannerTaskViewModel.Title);
                    break;

                case nameof(plannerTaskViewModel.BucketId):
                    string bucketName = string.Empty;
                    foreach (PlannerBucket bucket in Buckets)
                    {
                        if (bucket.Id == plannerTaskViewModel.BucketId)
                        {
                            bucketName = bucket.Name;
                            break;
                        }
                    }

                    plannerTaskViewModel.BucketName = bucketName;
                    task.Add(TaskBucketIdJsonName, plannerTaskViewModel.BucketId);
                    if (string.IsNullOrEmpty(plannerTaskViewModel.BucketId))
                    {
                        skipUpdate = true;
                    }

                    break;

                case nameof(plannerTaskViewModel.DueDateTime):
                    task.Add(TaskDueDateTimeJsonName, plannerTaskViewModel.DueDateTime);
                    if (_list.ContainerFromItem(plannerTaskViewModel) is ListViewItem taskContainer)
                    {
                        var flyout = taskContainer.ContentTemplateRoot.FindDescendants <Button>().FirstOrDefault(s => s.Flyout != null)?.Flyout;
                        if (flyout != null)
                        {
                            flyout.Hide();
                        }
                    }

                    break;

                default:
                    skipUpdate = true;
                    break;
                }

                if (!skipUpdate)
                {
                    plannerTaskViewModel.IsUpdating = true;
                    try
                    {
                        GraphServiceClient graphClient = await GraphServiceHelper.GetGraphServiceClientAsync();

                        if (graphClient != null)
                        {
                            PlannerTask taskToUpdate = await graphClient.Planner.Tasks[plannerTaskViewModel.Id].Request().GetAsync();
                            if (task.ContainsKey(TaskAssignmentsJsonName))
                            {
                                PlannerAssignments assignments  = task[TaskAssignmentsJsonName] as PlannerAssignments;
                                string[]           oldAssignees = taskToUpdate.Assignments.Assignees.ToArray();
                                string[]           newAssignees = assignments.Assignees.ToArray();
                                foreach (string userId in oldAssignees)
                                {
                                    if (!newAssignees.Contains(userId))
                                    {
                                        assignments.AddAssignee(userId);
                                        assignments[userId] = null;
                                    }
                                }
                            }

                            plannerTaskViewModel.ETag = taskToUpdate.GetEtag();
                            using (HttpRequestMessage request = graphClient.Planner.Tasks[plannerTaskViewModel.Id].Request().GetHttpRequestMessage())
                            {
                                request.Method = new HttpMethod(HttpMethodPatch);
                                string json = JsonConvert.SerializeObject(task);
                                request.Content = new StringContent(json, System.Text.Encoding.UTF8, MediaTypeJson);
                                request.Headers.Add(HttpHeaderIfMatch, plannerTaskViewModel.ETag);
                                await graphClient.AuthenticationProvider.AuthenticateRequestAsync(request);

                                HttpResponseMessage response = await graphClient.HttpProvider.SendAsync(request);

                                response.Dispose();
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        MessageDialog messageDialog = new MessageDialog(exception.Message);
                        await messageDialog.ShowAsync();
                    }

                    plannerTaskViewModel.IsUpdating = false;
                }
            }
        }