API更新Sharepoint中的多项选择字段

API更新Sharepoint中的多项选择字段

本文介绍了使用Rest API更新Sharepoint中的多项选择字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用rest api在线更新sharepoint中的多项选择字段.我收到401错误的请求错误.

I am trying to update the multiple choice field in sharepoint online using rest api. I am getting 401 bad request error.

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("XXXXX/_api/web/getfilebyserverrelativeurl('/sites/Sample/TestDoc.docx')/ListItemAllFields/");

string stringData =
@"{'__metadata': { 'type':'SP.ListItem' },
'TestColumn': { '__metadata': { 'type' : 'Collection(Edm.String)', results: ['Test1']}}}";

request.ContentLength = stringData.Length;

StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(stringData);
writer.Flush();

 response = (HttpWebResponse)request.GetResponse();

有帮助吗?

推荐答案

在您的示例中,有效负载很可能是无效:

Most likely the payload is invalid in your example:

string stringData =
@"{'__metadata': { 'type':'SP.ListItem' },
'TestColumn': { '__metadata': { 'type' : 'Collection(Edm.String)', results: ['Test1']}}}";

关键是类型SP.ListItem对应于Generic列表,在您的示例中,它是 Documents 库.

The point is the type SP.ListItem corresponds to Generic list, in your example it is a Documents library.

因此,需要提供一个 valid 实体类型名称,您可以利用以下端点来确定metadata type:

So, a valid entity type name needs to be provided, you could utilize the following endpoint to determine metadata type:

端点:

Url: /_api/lists/getbytitle('<list title>')?$select=ListItemEntityTypeFullName
Method: GET

第二,更新操作要求在请求中指定以下属性:

Secondly, the Update operation requires the following properties to be specified with request:

  • 使用POST动词创建HTTP请求.
  • 添加一个X-HTTP-Method标头,其值为MERGE.
  • 添加具有实体原始ETag值的If-Match标头.
  • Create an HTTP request using the POST verb.
  • Add an X-HTTP-Method header with a value of MERGE.
  • Add an If-Match header with a value of the entity’s original ETag.

您可以参考这篇文章以获取更多详细信息.

You could refer this post for a more details.

最后但并非最不重要的一点是,需要指定Content-TypeAccept请求标头(遵循这篇文章了解更多详细信息),例如:

And last but no least, Content-Type and Accept request headers needs to be specified (follow this article for a more details), for example:

request.Accept = "application/json;odata=verbose";
request.ContentType = "application/json;odata=verbose";


下面的示例对其进行了总结,并演示了如何更新多项选择字段的值:


The following example summarizes it and demonstrates how to update multi-choice field value:

var requestUrl = $"{webUrl}/_api/web/getfilebyserverrelativeurl('{fileUrl}')/ListItemAllFields";
var request = (HttpWebRequest)WebRequest.Create(requestUrl);
request.Credentials = GetCredentials(userName, password);
request.Accept = "application/json;odata=verbose";
request.ContentType = "application/json;odata=verbose";
request.Method = "POST";
request.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
request.Headers.Add("X-RequestDigest", formDigestVal);
request.Headers.Add("X-HTTP-Method", "MERGE");
request.Headers.Add("If-Match", "*");


var payload = @"{
                        '__metadata': { 'type':'SP.Data.Shared_x0020_DocumentsItem' },
                        '<ColumnName>': { '__metadata': { 'type' : 'Collection(Edm.String)'}, results: ['Val1']}
        }";

request.ContentLength = payload.Length;
var writer = new StreamWriter(request.GetRequestStream());
writer.Write(payload);
writer.Close();

var response = (HttpWebResponse)request.GetResponse();

这篇关于使用Rest API更新Sharepoint中的多项选择字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 00:27