【问题标题】:Sharepoint REST set metadata after document upload文档上传后 Sharepoint REST 设置元数据
【发布时间】:2014-11-06 09:59:50
【问题描述】:

我有一个 C# 控制台应用程序(完成后最终将成为 Windows 服务)。目的是将文档存储(500k+ 文档)迁移到 SharePoint 2013。

注意,这是一个独立的 C# 应用程序,将在未安装 SharePoint 的服务器上运行。我通过 HttpClient 调用使用纯 REST。

上传文档并设置其元数据有 3 个步骤:

  1. 上传文件(POST)

    例如baseURL + "web/" + "GetFolderByServerRelativeUrl('/sites" + libPath + "')/Files/Add(url='" + filename + "',overwrite=true)";

  2. 检索与此文档关联的列表项的详细信息 (GET)

    例如baseURL + "web/lists/getbytitle('" + docLibName + "')/items?$filter=Title eq '" + filenameNoExt + "'"

前两个步骤运行良好。

  1. 更新元数据

这就是让我现在彻夜难眠的一点。我似乎无法弄清楚我应该向网络服务发送什么,而我尝试的任何东西都会出错。我找不到任何试图这样做的人的例子;并且 MS 文档非常稀少。

http://msdn.microsoft.com/en-us/library/office/dn292552%28v=office.15%29.aspx 的 MS 文档说:

以下示例显示如何使用 MERGE 方法更新列表。

url: http: //siteurl/_api/web/lists(guid'list GUID') method: POST body: { '__metadata': { 'type': 'SP.List' }, 'Title': 'New title' } Headers: Authorization: "Bearer " + accessToken X-RequestDigest: form digest value IF-MATCH": etag or "*" X-HTTP-Method: MERGE, accept: "application/json;odata=verbose" content-type: "application/json;odata=verbose" content-length:length of post body

在我的应用程序中,我已将查询中的类型设置为 SP.Data.CertificatesItem,这就是步骤 2 中返回的内容;我正在尝试将证书类型字段设置为指定的值,而不是文档标题:

string body = "{ '__metadata': { 'type': '" + spItemType + "' }, 'CertificateType': 'Medical Certificate'}";

(有几个字段需要设置,我这里只是硬编码一个字段进行测试。需要设置的字段取决于文档库的内容类型。)

此外,网址略有不同。该示例的位置:

http: //siteurl/_api/web/lists(guid'list GUID')

我有(具体例子,略匿名):

http: //siteurl/_api/Web/Lists(guid'cdcbef76-8bc0-4a68-9279-f6f3b6cbd3c3')/Items(2)

否则,我的调用与上面的示例相同。

我收到错误消息:

{"error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"The parameter __metadata does not exist in method GetById."}}}

我不明白我做错了什么!而且我在任何地方都找不到任何具体的例子。

来自页面:http://msdn.microsoft.com/en-us/library/office/dn292553%28v=office.15%29.aspx

If you want to update a file's metadata, you'll have to construct an endpoint that reaches the file as a list item. You can do this because each folder is also a list, and each file is also a list item. Construct an endpoint that looks like this: https: //siteurl/_api/web/lists/getbytitle('Documents')/items(). Working with lists and list items with REST explains how to update a list item's metadata.

谁能在这里提供任何见解?

更新:

似乎 X-HTTP-Method 设置不正确。我解决了这个问题,现在我得到了:

{"error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"An unexpected 'PrimitiveValue' node was found when reading from the JSON reader. A 'StartObject' node was expected."}}}

【问题讨论】:

    标签: c# rest sharepoint


    【解决方案1】:

    我自己研究这个问题时遇到了你的问题。我已经记录了如何在 .NET 中使用 HttpClient 进行文件上传、创建、更新和检索列表项,包括那些带有 Termstore 数据的列表项,而无需 JQuery。

    使用 REST 从 HttpClient 更新文件的示例:

    client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true }); client.BaseAddress = new System.Uri(url); client.DefaultRequestHeaders.Clear(); client.DefaultRequestHeaders.Add("X-RequestDigest", digest); client.DefaultRequestHeaders.Add("X-HTTP-Method", "MERGE"); client.DefaultRequestHeaders.Add("IF-MATCH", "*"); HttpContent strContent = new StringContent(String.Concat("{ '__metadata': { 'type': 'SP.List' }, 'Title': '", filename, "' }")); strContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); strContent.Headers.ContentType.Parameters.Add(new NameValueHeaderValue("odata", "verbose")); HttpResponseMessage updateResponse = await client.PostAsync(String.Concat("_api/web/lists/GetByTitle('Project Photos')/Items(", id, ")"), strContent); updateResponse.EnsureSuccessStatusCode(); if (updateResponse.IsSuccessStatusCode) {}

    https://arcandotnet.wordpress.com/2015/04/01/sharepoint-2013-rest-services-using-c-and-the-httpclient-for-windows-store-apps/

    希望这会有所帮助。

    Arcan.NET

    【讨论】:

    • 请在回答中添加相关部分以回答问题,记住链接可能会过时或损坏。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-16
    • 1970-01-01
    相关资源
    最近更新 更多