1

We have been trying to add a document to a list item using Graph API but are being thrown a 400 error :( We have the access token and can query the list in question without any problem. Below is the code we are using to try to add the document to the list

string addItemJsonString = "{\"fields\":{\"Title\":\"Dec Word file\"}}";

string requestUrl = "https://graph.microsoft.com/v1.0/sites/mysite.sharepoint.com:/sites/mySite:/lists/My Library/items";

HttpClient client = new HttpClient();

HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post, requestUrl);
message.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
message.Content = new StringContent(addItemJsonString, Encoding.UTF8, "application/json"); //set the body for the request


client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8");

HttpResponseMessage response = await client.SendAsync(message);

if (response.IsSuccessStatusCode)
{
  responseString = await response.Content.ReadAsStringAsync();
}
else
  responseString = "Error in response";

Any idea of where we are losing it?

  • Do you want to add a list item to a list, or upload a document as your title describes? – Helge S Dec 22 '17 at 12:04
  • We want to upload a document as an attachment to the list item. The use case is that the list item describes the searchable meta data of the document. – Muhammad Khurram Bhatti Dec 23 '17 at 13:41
  • Please consider using a document set or a document library to provide metadata to documents instead. I'm not familiar with your specific use-case, but my very best advice to you is to as far as it's possible, avoid list item attachments for file storage. – Helge S Dec 26 '17 at 16:02
  • We can use a document library. I always thought the two use the same structure. Do you think the above code will work if we change the underlying structure from list to library? – Muhammad Khurram Bhatti Dec 26 '17 at 16:13
0

In reference to the comments, if you want to upload a document to a SharePoint Online Document Library using Microsoft Graph, you should start with the ListItem endpoint.

Note the end of the documentation where it specifies that for document libraries, a ListItem is exposed as a driveItem.

To upload to the driveItem, you should have two options. If your files are less than 4MB, issue a PUT /sites/{site-id}/drive/items/{item-id}/content with the binary contents in the body of the request. This is documented here.

For files over 4MB, you must use establish an UploadSession as documented here. Basically, you issue a POST /sites/{siteId}/drive/items/{itemId}/createUploadSession to create the session, and an uploadUrl will be returned to you. You can then issue a

PUT https://<uplod url>
Content-Length: 26
Content-Range: bytes 0-25/128

<bytes 0-25 of the file>
| improve this answer | |
  • Helge my friend - We have incorporated your suggestion but still are running into bad request exception. One thing that we are not clear about is {parent-id} attribute of the request. What exactly needs to replace this tag? This maybe where we are losing it. – Muhammad Khurram Bhatti Dec 29 '17 at 12:25
0

To upload an small item (<4MB) into a SharePoint Online document library or list which is not the default library, you can use the following URL syntax:

https://graph.microsoft.com/v1.0/sites/{SITE-ID}/drives/{DRIVE-ID}/root:/{FILENAME}:/content 
  • {SITE-ID}: the ID of SP site
  • {DRIVE-ID}: the ID of drive (document library or list)
  • {FILENAME}: the name of the fie to upload

The "/root:" after the drive ID is the important point.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.