在图形API中使用GUID获取共享列表项 [英] Getting a sharepoint listitem using GUID in graph api

查看:67
本文介绍了在图形API中使用GUID获取共享列表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们需要将文档上传到SharePoint Online列表,然后将一些可搜索的属性添加到文档中.我们正在使用Microsoft Graph上传文档.

We need to upload a document to a SharePoint Online list and then add some searchable attributes to the document. We are uploading the document using Microsoft Graph.

由于我们需要能够根据不同的条件搜索文档,因此我们将文档上载到列表中.

Since we need to be able to search for the document against different criteria, we are uploading the document to a List.

这是请求的快照:

string requestUrl = "https://graph.microsoft.com/v1.0/drives/" +
    driveID +
    "/items/root:/" +
    fileName +
    ".docx:/content";

HttpClient client = new HttpClient();

HttpRequestMessage message =
    new HttpRequestMessage(HttpMethod.Put, requestUrl);
message.Headers.Authorization =
    new AuthenticationHeaderValue("Bearer", accessToken);

message.Content = new StreamContent(requestContent);
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type",
    "application/json; odata=verbose; charset=utf-8");

HttpResponseMessage response = await client.SendAsync(message);

上载文档后,我们将返回包含GUID和文档名称的响应.

When the document gets uploaded we get back the response that includes a GUID and the document name.

{
  "createdDateTime": "2018-02-27T10:44:02Z",
  "eTag": "\"{446F157D-ED2C-4C1B-BB3F-56897A6190DA},1\"",
  "id": "01JSWLMSD5CVNHFLHNDNGLWP2WGNNGDEG2",
  "lastModifiedDateTime": "2018-02-27T10:44:02Z",
  "name": "2018-02-27 15.43.55.7268PM.docx"
}

当我们试图保留该文档以填充列表中的其他列时,就出现了窍门.

The trick comes when we try to get a hold of that document to add populate the other columns in the List.

我们可以使用列表中文档的id字段来获取文档,但是当我们上传文档时,id不会作为响应的一部分返回,因此不能使用该值进行请求.

We can get the document by using the id field of the document in the list but that id is not returned as part of the response when we upload the document so cannot use that value for our request.

我们的挑战是使用从JSON响应中获取的字段获取ListItem,但是到目前为止,我们尝试的每种途径都导致失败.

Our challenge is to get the ListItem using the fields that we get back from the JSON response but so far every avenue we have tried has resulted in a failure.

我们尝试使用上面响应中提到的idname字段,但是得到了HTTP 400响应.以下是我们尝试的多种版本的请求.

We have tried using both id and name fields mentioned in the response above but are getting an HTTP 400 response. Below are the requests we have tried in multiple variations.

.../lists/My Library/items?expand=fields&filter=fields/name eq '2018-02-27 15.43.55.7268PM.docx'

...:/lists/My Library/items?filter=id eq '01JSWLMSD5CVXUILHDNGLWP2WGNNGDEG2' 

我们在哪里丢失它?

推荐答案

好,所以我们可以将项目添加到SharePoint列表的唯一方法是将其添加为驱动器项目.因此,我们返回的GUID是驱动器项而不是列表项.为了更新列表项元数据或更新列表项的字段,您需要获取列表项ID.非常感谢,到目前为止,Microsoft维护驱动器项目的"SharePointIds"资源类型.您需要做的是使用上载文档后获得的驱动器驱动器项目GUID搜索此资源,然后在响应中使用列表项" ID的值来更新列表项值.以下是获取驱动器项目的SharePointId所提出的请求的语法.

Ok, so the only way we can add an item to a SharePoint List is by adding it as a drive item. Consequently the GUID that we get back is of a drive item and not of a list item. In order to update the list item meta data or to update the fields for the list item you need to get the list item id. Thanks a lot that so far Microsoft maintains the "SharePointIds" resource type for the drive item. What you need to do is search for this resource using the drive drive item GUID that you get after uploading your document and then use the value of "list item" id within the response to update the list item values. Below is the syntax for the request you will make to get the SharePointId of the drive item.

    https://graph.microsoft.com/v1.0/drives/MyDriveId/items/MyItemGUID?$select=sharepointids 

这给了我以下响应,其中包括列表项ID:

This gave me following response that includes list item id:

    {
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#drives('b%21Fxup9FZ-HUOeolxFVUS8xtUFzOnGdmJFmOu-plkSayoX0IRAx4QQLGVWwqhin-F')/items(sharepointIds)/$entity",
"@odata.etag": "\"{B7B82A72-72EF-414A-8C1E-0A26896HDC79},6\"",
"sharepointIds": {
    "listId": "11425fa8-1e03-4010-b195-5b0as83a7f85",
    "listItemId": "1178",
    "listItemUniqueId": "b7b82a72-72ef-414a-8c1e-0a24532ddc79",
    "siteId": "f4a91b17-7e56-431d-9ea2-5c589644bcc6",
    "siteUrl": "https://Mywesite.com/sites/MyLibrary",
    "webId": "e9cc05d5-76c6-4562-98eb-bea33a98742c"
} } 

非常感谢@marc指向尝试使用GUID尝试获取驱动器ID来保留该项目.我们现在处于绿色状态:)

Thanks a lot @marc for pointing towards trying the drive id to get a hold of the item using GUID. We are in the green now :)

这篇关于在图形API中使用GUID获取共享列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆