【问题标题】:How to PATCH data using System.Net.Http如何使用 System.Net.Http 修补数据
【发布时间】:2019-05-27 16:09:23
【问题描述】:

我已将文件上传到 SharePoint 并找出它的 ID。现在我需要更新该列表项上的其他一些列。问题是 System.Net.Http.HttpMethod.Patch 不存在。

public static async Task<string> UpdateFileData()
{
    var (authResult, message) = await Authentication.AquireTokenAsync();

    string updateurl = MainPage.rooturl + "lists/edd49389-7edb-41db-80bd-c8493234eafa/items/" + fileID + "/";
    var httpClient = new HttpClient();
    HttpResponseMessage response;
    try
    {
        var root = new
        {
            fields = new Dictionary<string, string>
            {
                { "IBX", App.IBX },  //column to update
                { "Year", App.Year}, //column to update
                { "Month", App.Month} //column to update
            }
        };

        var s = new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat };
        var content = JsonConvert.SerializeObject(root, s);
        var request = new HttpRequestMessage(HttpMethod.Put, updateurl);
        request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authResult.AccessToken);
        request.Content = new StringContent(content, Encoding.UTF8, "application/json");
        response = await httpClient.SendAsync(request);
        var responseString = await response.Content.ReadAsStringAsync();
        return responseString;
    }
    catch (Exception ex)
    {
        return ex.ToString();
    }
}

【问题讨论】:

    标签: c# sharepoint microsoft-graph-api


    【解决方案1】:

    修改代码如下。

    public static async Task<string> UpdateFileData()
    {
        var (authResult, message) = await Authentication.AquireTokenAsync();
    
        string updateurl = MainPage.rooturl + "lists/edd49389-7edb-41db-80bd-c8493234eafa/items/" + fileID + "/";
        var httpClient = new HttpClient();
        HttpResponseMessage response;
        try
        {
            var root = new
            {
                fields = new Dictionary<string, string>
                {
                    { "IBX", App.IBX },  //column to update
                    { "Year", App.Year}, //column to update
                    { "Month", App.Month} //column to update
                }
            };
    
            var s = new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat };
            var content = JsonConvert.SerializeObject(root, s);
            var request = new HttpRequestMessage(new HttpMethod("PATCH"), updateurl);
            request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authResult.AccessToken);
            request.Content = new StringContent(content, System.Text.Encoding.UTF8, "application/json;odata=verbose");
            response = await httpClient.SendAsync(request);
            var responseString = await response.Content.ReadAsStringAsync();
            return responseString;
        }
        catch (Exception ex)
        {
            return ex.ToString();
        }
    }
    

    或者我们也可以使用 REST API 按 ID 更新列表项。

    参考:SharePoint 2013 REST Services using C# and the HttpClient

    【讨论】:

      【解决方案2】:

      这取决于是使用.NET Core 还是.NET Framework,如果可以使用`.NET Core HttpClient.PatchAsync Method

      如果是.NET Framework ListItem 可以这样更新:

      using (var client = new HttpClient())
      {
           client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
           client.BaseAddress = new Uri("https://graph.microsoft.com");
      
           var listItemPayload = new Dictionary<string, object>
           {
              {"Color", "Fuchsia"},
              {"Quantity", 934}
           };
      
           var requestContent = new StringContent(JsonConvert.SerializeObject(listItemPayload));
           requestContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
           var response = await client.PatchAsync(new Uri($"https://graph.microsoft.com/v1.0/sites/{siteId}/lists/{listId}/items/{itemId}/fields"), requestContent);
           var data = response.Content.ReadAsStringAsync().Result.ToString();
      }
      

      其中PatchAsyncHttpClient class扩展 方法:

      public static class HttpClientExtensions
      {
          public static async Task<HttpResponseMessage> PatchAsync(this HttpClient client, Uri requestUri, HttpContent iContent)
          {
              var method = new HttpMethod("PATCH");
              var request = new HttpRequestMessage(method, requestUri)
              {
                  Content = iContent
              };
      
              HttpResponseMessage response = new HttpResponseMessage();
              try
              {
                  response = await client.SendAsync(request);
              }
              catch (TaskCanceledException e)
              {
                  Debug.WriteLine("ERROR: " + e.ToString());
              }
      
              return response;
          }
      }
      

      扩展方法的所有功劳归this answer的作者所有@

      【讨论】:

      • 我无法在 .Net Framework 4.3.4 版中使用 PatchSync() 方法
      【解决方案3】:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-02-12
        • 2014-09-02
        • 2012-04-24
        • 2021-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多