Getting a sharepoint listitem using GUID in graph

2019-07-20 07:46发布

问题:

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.

Here is a snapshot of the request:

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);

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.

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.

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.

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'

and

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

Where are we losing it?

回答1:

To retrieve the item you need to include the id in in the URL itself:

/sites/{id}/lists/{id}/items/01JSWLMSD5CVXUILHDNGLWP2WGNNGDEG2?$expand=fields

I would also suggest using the Microsoft Graph Client Library for .NET instead of rolling your own using HttpClient.



回答2:

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 

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"
} } 

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 :)