コード例 #1
0
        public async Task UpdateTask(SpTask task)
        {
            StringBuilder requestUri = new StringBuilder(SettingsHelper.SharePointServiceEndpoint)
                                       .Append("/_api/web/lists/getByTitle('Tasks')/items")
                                       .Append("(" + task.Id + ")");

            var newTaskJson = new SpTaskJson {
                __metadata = new __Metadata {
                    Type = "SP.Data.TasksListItem"
                },
                Title    = task.Title,
                Status   = task.Status,
                Priority = task.Priority
            };

            StringContent requestContent = new StringContent(JsonConvert.SerializeObject(
                                                                 newTaskJson,
                                                                 Formatting.None,
                                                                 new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            requestContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json;odata=verbose");

            HttpClient         client  = new HttpClient();
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUri.ToString());

            request.Headers.Add("ACCEPT", "application/json;odata=verbose");
            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await GetAccessToken());
            request.Content = requestContent;
            request.Headers.Add("IF-MATCH", "*");
            request.Headers.Add("X-HTTP-Method", "MERGE");

            await client.SendAsync(request);
        }
コード例 #2
0
    public async Task CreateTask(SpTask task) {
      StringBuilder requestUri = new StringBuilder(SettingsHelper.SharePointServiceEndpoint)
          .Append("/_api/web/lists/getByTitle('Tasks')/items");

      var newTaskJson = new SpTaskJson {
        __metadata = new __Metadata { Type = "SP.Data.TasksListItem" },
        Title = task.Title,
        Status = task.Status,
        Priority = task.Priority
      };

      StringContent requestContent = new StringContent(JsonConvert.SerializeObject(
        newTaskJson,
        Formatting.None,
        new JsonSerializerSettings {
          NullValueHandling = NullValueHandling.Ignore
        }));
      requestContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json;odata=verbose");

      HttpClient client = new HttpClient();
      HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUri.ToString());
      request.Headers.Add("ACCEPT", "application/json;odata=verbose");
      request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await GetAccessToken());
      request.Content = requestContent;

      await client.SendAsync(request);
    }