SharePoint 2013 REST API:更新文件元数据

时间:2016-08-02 19:38:10

标签: c# json rest sharepoint sharepoint-2013

我正在尝试修改刚刚上传的文件的元数据。

using (HttpClient client = new HttpClient(new HttpClientHandler { Credentials = _authenticator.Credential }))
{
    client.DefaultRequestHeaders.Add("X-HTTP-Method", "MERGE");
    client.DefaultRequestHeaders.Add("Accept", "application/json;odata=verbose");
    client.DefaultRequestHeaders.Add("X-RequestDigest", _authenticator.Token);
    client.BaseAddress = _authenticator.Endpoint;
    client.DefaultRequestHeaders.Add("IF-MATCH", "*");

    string cmd = String.Format("_api/web/lists/GetByTitle('Drop Off Library')/items({0})", file.Id);

    string jsonString = JsonConvert.SerializeObject(file);
    StringContent test = new StringContent(jsonString, Encoding.UTF8, "application/json");

    HttpResponseMessage response = await client.PostAsync(cmd, test);
}

我成功获取上面的文件元数据,并将其存储在我自己创建的SharePoint文件模型中。我修改了文件的一个元数据字段,然后尝试重新合并反序列化的对象。这导致400 Bad Request错误。有关为什么会发生这种情况的任何想法?

1 个答案:

答案 0 :(得分:1)

解决方案是更换线路:

string jsonString = JsonConvert.SerializeObject(file);
StringContent test = new StringContent(jsonString, Encoding.UTF8, "application/json");

var test = new StringContent(JsonConvert.SerializeObject(payload));
test.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json;odata=verbose");

因为在您的情况下会生成无效的 Content-Type标头。

enter image description here

有关支持的格式列表,请参阅JSON Light support in REST SharePoint API released