c# - 如何使用 Microsoft.Graph 将文件附加到 Sharepoint 中的项目

标签 c# sharepoint microsoft-graph-api

Microsoft.Graph Sharepoint api 允许更新列表项 https://developer.microsoft.com/en-us/graph/docs/api-reference/beta/api/listitem_update使用 PATCH 请求。但是如何生成正确的请求呢?

    using (HttpClient pacthClient = new HttpClient())
    {
        var mediaType = new MediaTypeWithQualityHeaderValue("application/json");
        pacthClient.DefaultRequestHeaders.Accept.Add(mediaType);
        pacthClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", userToken);
        using (HttpRequestMessage requestMessage = new HttpRequestMessage(new HttpMethod("PATCH"), $"{uri}/{id}"))
        {
            requestMessage.Content = byteArrayContent ???
            using (HttpResponseMessage responseMessage = await pacthClient.SendAsync(requestMessage))
            {
            }
        }
    }

最佳答案

 string addItemJsonString = "{\"name\":\"Test Visit\"}";

        string requestUrl = "https://graph.microsoft.com/v1.0/sites/{siteID}/lists/{listID}/items/{itemID}/fields";


        HttpClient client = new HttpClient();

        HttpRequestMessage message = new HttpRequestMessage(new HttpMethod("PATCH"), requestUrl);
        message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        message.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

        //set the request body
        message.Content = new StringContent(addItemJsonString);


        HttpResponseMessage response = await client.SendAsync(message);

        if (response.IsSuccessStatusCode)
        {
            responseString = await response.Content.ReadAsStringAsync();
        }
        else
            responseString = "Error in response";

关于c# - 如何使用 Microsoft.Graph 将文件附加到 Sharepoint 中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47810109/

相关文章:

结构和接口(interface)中的 C# getter/setter

c# - 在 StructureMap 4.0 中获取现有容器

c# - 在 Sharepoint 中下载文件后不再回发

security - Sharepoint:如何以编程方式管理 SPFolder 和 SPListItem 权限

python - 如何使用 microsoft graph api 选择来自特定电子邮件地址的所有电子邮件

c# - 正则表达式:不匹配单词列表

c# - 构造函数注入(inject)过度使用

file - 如何将带有特殊字符的文件名转换为有效的文件名?

microsoft-graph-api - 微软图形 : Unable to filter by binary singleValueExtendedProperty

c# - 使用 Microsoft Graph .NET 客户端库从组中删除成员