public async Task UpdatePlanREST(MyPlan myPlan)
        {
            var accessToken = await GraphAuthHelper.GetGraphAccessTokenAsync();

            var     restURL      = string.Format("{0}planner/plans/{1}", SettingsHelper.GraphResourceUrl, myPlan.id);
            dynamic postPlanJSON = new JObject();

            postPlanJSON.title = myPlan.title;
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    byte[]         btBodys = Encoding.UTF8.GetBytes(postPlanJSON.ToString());
                    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(restURL);
                    request.Method      = "PATCH";
                    request.Accept      = "application/json";
                    request.ContentType = "application/json";
                    request.Headers.Add("Authorization", "Bearer " + accessToken);
                    request.Headers.Add("If-Match", myPlan.Etag);
                    request.GetRequestStream().Write(btBodys, 0, btBodys.Length);
                    using (HttpWebResponse response = await request.GetResponseAsync() as HttpWebResponse)
                    {
                        if (response.StatusCode == HttpStatusCode.NoContent)
                        {
                            //update successfully
                        }
                    }
                }
            }
            catch (Exception el)
            {
                el.ToString();
            }
        }
        private async Task <string> CreateGroupREST(string groupTitle)
        {
            var accessToken = await GraphAuthHelper.GetGraphAccessTokenAsync();

            string  groupId   = string.Empty;
            dynamic groupJSON = new JObject();

            groupJSON.displayName     = groupTitle;
            groupJSON.mailNickname    = groupTitle.Replace(" ", "");
            groupJSON.securityEnabled = false;
            groupJSON.mailEnabled     = true;
            groupJSON.groupTypes      = new JArray("Unified");
            HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post, string.Format("{0}groups", SettingsHelper.GraphResourceUrl));

            message.Content = new StringContent(groupJSON.ToString(), System.Text.Encoding.UTF8, "application/json");
            message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            message.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
            using (HttpClient client = new HttpClient())
            {
                var responseMessage = await client.SendAsync(message);

                if (responseMessage.StatusCode != System.Net.HttpStatusCode.Created)
                {
                    throw new Exception(responseMessage.StatusCode.ToString());
                }

                var payload = await responseMessage.Content.ReadAsStringAsync();

                groupId = JObject.Parse(payload)["id"].ToString();

                await AddMemberForGroup(groupId);
            }

            return(groupId);
        }
        private async Task AddMemberForGroup(string groupid)
        {
            var accessToken = await GraphAuthHelper.GetGraphAccessTokenAsync();

            var    userObjectId          = ClaimsPrincipal.Current.FindFirst(SettingsHelper.ClaimTypeObjectIdentifier).Value;
            var    restURL               = string.Format("{0}groups('{1}')/members/$ref", SettingsHelper.GraphResourceUrl, groupid);
            string strAddMememberToGroup = "{\"@odata.id\":\"" + SettingsHelper.GraphResourceUrl + "users('" + userObjectId + "')\"}";

            try
            {
                using (HttpClient client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

                    var requestMessage = new HttpRequestMessage(HttpMethod.Post, restURL);
                    requestMessage.Content = new StringContent(strAddMememberToGroup, System.Text.Encoding.UTF8, "application/json");
                    using (var response = await client.SendAsync(requestMessage))
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            return;
                        }
                        else
                        {
                            throw new Exception("add memeber to group error: " + response.StatusCode);
                        }
                    }
                }
            }
            catch (Exception el)
            {
                el.ToString();
            }
        }
        public async Task DeleteTaskREST(string id, string eTag)
        {
            var accessToken = await GraphAuthHelper.GetGraphAccessTokenAsync();

            var restURL = string.Format("{0}planner/tasks/{1}", SettingsHelper.GraphResourceUrl, id);

            try
            {
                using (HttpClient client = new HttpClient())
                {
                    var accept = "application/json";

                    client.DefaultRequestHeaders.Add("Accept", accept);
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
                    client.DefaultRequestHeaders.Add("If-Match", eTag);

                    using (var response = await client.DeleteAsync(restURL))
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            return;
                        }
                        else
                        {
                            throw new Exception("delete task error: " + response.StatusCode);
                        }
                    }
                }
            }
            catch (Exception el)
            {
                el.ToString();
            }
        }
Exemple #5
0
        public static async Task <GraphServiceClient> GetGraphServiceAsync()
        {
            var accessToken = await GraphAuthHelper.GetGraphAccessTokenAsync();

            var graphserviceClient = new GraphServiceClient(GraphResourceUrl,
                                                            new DelegateAuthenticationProvider(
                                                                (requestMessage) =>
            {
                requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
                return(Task.FromResult(0));
            }));

            return(graphserviceClient);
        }
        public async Task <List <MyPlan> > GetPlansREST()
        {
            var plansResult = new List <MyPlan>();
            var accessToken = await GraphAuthHelper.GetGraphAccessTokenAsync();

            var restURL = string.Format("{0}me/planner/plans/", SettingsHelper.GraphResourceUrl);

            try
            {
                using (HttpClient client = new HttpClient())
                {
                    var accept = "application/json";

                    client.DefaultRequestHeaders.Add("Accept", accept);
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

                    using (var response = await client.GetAsync(restURL))
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            var jsonresult = JObject.Parse(await response.Content.ReadAsStringAsync());

                            foreach (var item in jsonresult["value"])
                            {
                                plansResult.Add(new MyPlan
                                {
                                    id        = item["id"].ToString(),
                                    title     = item["title"].ToString(),
                                    owner     = !string.IsNullOrEmpty(item["owner"].ToString()) ? item["owner"].ToString() : "",
                                    createdBy = !string.IsNullOrEmpty(item["createdBy"].ToString()) ? item["createdBy"].ToString() : "",
                                    Etag      = !string.IsNullOrEmpty(item["@odata.etag"].ToString()) ? item["@odata.etag"].ToString() : ""
                                });
                            }
                        }
                    }
                }
            }
            catch (Exception el)
            {
                el.ToString();
            }

            return(plansResult);
        }
        public async Task <List <MyTask> > GetTasksREST(string planid)
        {
            var tasksResult = new List <MyTask>();
            var accessToken = await GraphAuthHelper.GetGraphAccessTokenAsync();

            var restURL = string.Format("{0}planner/plans/{1}/tasks", SettingsHelper.GraphResourceUrl, planid);

            try
            {
                using (HttpClient client = new HttpClient())
                {
                    var accept = "application/json";

                    client.DefaultRequestHeaders.Add("Accept", accept);
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

                    using (var response = await client.GetAsync(restURL))
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            var jsonresult = JObject.Parse(await response.Content.ReadAsStringAsync());

                            foreach (var item in jsonresult["value"])
                            {
                                tasksResult.Add(new MyTask
                                {
                                    id              = item["id"].ToString(),
                                    title           = item["title"].ToString(),
                                    percentComplete = !string.IsNullOrEmpty(item["percentComplete"].ToString()) ? Convert.ToInt32(item["percentComplete"].ToString()) : 0,
                                    planId          = planid,
                                    Etag            = !string.IsNullOrEmpty(item["@odata.etag"].ToString()) ? item["@odata.etag"].ToString() : ""
                                });
                            }
                        }
                    }
                }
            }
            catch (Exception el)
            {
                el.ToString();
            }

            return(tasksResult);
        }
        public async Task <MyTask> GetTaskREST(string id)
        {
            MyTask task        = new MyTask();
            var    accessToken = await GraphAuthHelper.GetGraphAccessTokenAsync();

            var restURL = string.Format("{0}planner/tasks/{1}", SettingsHelper.GraphResourceUrl, id);

            try
            {
                using (HttpClient client = new HttpClient())
                {
                    var accept = "application/json";

                    client.DefaultRequestHeaders.Add("Accept", accept);
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

                    using (var response = await client.GetAsync(restURL))
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            var item = JObject.Parse(await response.Content.ReadAsStringAsync());

                            if (item != null)
                            {
                                task.id              = item["id"].ToString();
                                task.title           = item["title"].ToString();
                                task.percentComplete = !string.IsNullOrEmpty(item["percentComplete"].ToString()) ? Convert.ToInt32(item["percentComplete"].ToString()) : 0;
                                task.planId          = item["planId"].ToString();
                                task.Etag            = !string.IsNullOrEmpty(item["@odata.etag"].ToString()) ? item["@odata.etag"].ToString() : "";
                            }
                        }
                    }
                }
            }
            catch (Exception el)
            {
                el.ToString();
            }

            return(task);
        }
        public async Task <MyPlan> GetPlanREST(string id)
        {
            MyPlan plan        = new MyPlan();
            var    accessToken = await GraphAuthHelper.GetGraphAccessTokenAsync();

            var restURL = string.Format("{0}planner/plans/{1}", SettingsHelper.GraphResourceUrl, id);

            try
            {
                using (HttpClient client = new HttpClient())
                {
                    var accept = "application/json";

                    client.DefaultRequestHeaders.Add("Accept", accept);
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

                    using (var response = await client.GetAsync(restURL))
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            var item = JObject.Parse(await response.Content.ReadAsStringAsync());

                            if (item != null)
                            {
                                plan.title = !string.IsNullOrEmpty(item["title"].ToString()) ? item["title"].ToString() : string.Empty;
                                plan.Etag  = !string.IsNullOrEmpty(item["@odata.etag"].ToString()) ? item["@odata.etag"].ToString() : "";
                            }
                        }
                    }
                }
            }
            catch (Exception el)
            {
                el.ToString();
            }

            return(plan);
        }
        public async Task CreateTaskREST(MyTask myTask)
        {
            var accessToken = await GraphAuthHelper.GetGraphAccessTokenAsync();

            var     restURL      = string.Format("{0}/planner/tasks", SettingsHelper.GraphResourceUrl);
            dynamic postTaskJSON = new JObject();

            postTaskJSON.title           = myTask.title;
            postTaskJSON.percentComplete = myTask.percentComplete;
            postTaskJSON.planId          = myTask.planId;
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);


                    var requestMessage = new HttpRequestMessage(HttpMethod.Post, restURL);
                    requestMessage.Content = new StringContent(postTaskJSON.ToString(), System.Text.Encoding.UTF8, "application/json");
                    using (var response = await client.SendAsync(requestMessage))
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            return;
                        }
                        else
                        {
                            throw new Exception("add task error: " + response.StatusCode);
                        }
                    }
                }
            }
            catch (Exception el)
            {
                el.ToString();
            }
        }