我可以使用Microsoft Graph更新SharePoint文档的元数据吗?

时间:2019-07-16 15:25:19

标签: sharepoint microsoft-graph metadata

我正在使用Graph在SharePoint中创建文件夹和文档,但无法弄清楚如何更新文档的元数据。也许我似乎找不到任何细节。

我用Json消息中的元数据尝试了一个补丁命令,但没有用

我可以使用此代码创建文件,因此可以进行身份​​验证。

    public static string UploadContent(string webApiUrl, string accessToken, byte[] data)
    {
        string s = String.Empty;
        if (!string.IsNullOrEmpty(accessToken))
        {
            using (var client = new WebClient())
            {
                client.Headers.Add(HttpRequestHeader.Accept, "application/json");
                client.Headers.Add(HttpRequestHeader.Authorization, accessToken);
                s = client.Encoding.GetString(client.UploadData(webApiUrl, "PUT", data));

            }
        }

        return s;
    }

1 个答案:

答案 0 :(得分:0)

文件上传后,需要利用Update listItem endpoint更新listItem的属性,例如:

a)上传文件:

PUT  https://graph.microsoft.com/v1.0/drives/{drive-id}/root:/{file-path}:/content
Body: file content

b)更新列表项:

PATCH https://graph.microsoft.com/v1.0/drives/{drive-id}/root:/{file-path}:/listItem/fields   
Content-Type: application/json
Body {
    "Title": "new title"
}

C#示例

using (var client = new System.Net.WebClient())
{
    client.BaseAddress = "https://graph.microsoft.com/";
    client.Headers.Add(HttpRequestHeader.Accept, "application/json");
    client.Headers.Add(HttpRequestHeader.Authorization, accessToken);

    //1.Upload a new file
    var requestUrl = $"/v1.0/drives/{driveId}/root:/{filePath}:/content";
    var result = client.UploadData(requestUrl, "PUT", contentBytes);

    //2.Set file metadata
    requestUrl = $"/v1.0/drives/{driveId}/root:/{filePath}:/listItem/fields";
    var fields = new { Title = "Sample video" };
    var fieldsPayload = JsonConvert.SerializeObject(fields);
    client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
    client.UploadString(requestUrl, "PATCH", fieldsPayload);

}