Using the Microsoft Graph API to upload files to a Library with more than 5000 items gives you a HTTP 404
UPDATE: Around the time of this blog post I added a question on StackOverflow about this issue, and a few months later it was responded to and resolved. This issue in the format mentioned below should no longer exist. I have decided to keep the post alive however as it may support any other issues now or in the future. The Microsoft Graph provides a very easy method to upload files to OneDrive or SharePoint via the DriveItems resource. There are two methods to upload a file vis the Microsoft Graph:
- A straight PUT request to a location
- Via an upload session (a resumable upload)
The PUT request accepts files up to 4MB in size; all others need to be run via creating an upload session and sending up the file in chunks or as a whole to a URL returned after creating the upload session. You are perfectly capable of using the upload session to upload files less than 4MB in size. Given that a resumable upload works for all file sizes, I can’t see much reason to use the straight PUT request and would opt for a resumable upload to save my files to OneDrive or SharePoint. Except… When creating an upload session to a SharePoint document library with more than 5000 items (the SharePoint list threshold), and where the file already exists, we would get the following response:
The remote server returned an error: (404) Not Found.
{
"error": {
"code": "itemNotFound",
"message": "The resource could not be found.",
"innerError": {
"request-id": "6f31b3ee-2003-43aa-8956-99c82367c770",
"date": "2018-02-13T01:29:41"
}
}
}
This is obviously confusing, as the file actually does exist. In fact, in the situation where we are uploading to a library past the threshold but the file DOES NOT exist, everything uploads perfectly well. Note that this happens in the creating of the upload session, not the upload itself; this error occurs before the upload call can be made. You can add the following to the request body, and in fact, you have to if the file already exists; but there is no effect when the threshold has been exceeded.
{
"item": {
"@microsoft.graph.conflictBehavior": "replace"
}
}
I wanted to check if the straight put request (i.e. not the resumable upload) would upload and overwrite a file that exists in a library with more than 5000 items, and it does happily. The only error I did encounter was when the file exceeded 4MB:
The remote server returned an error: (413) Request Entity Too Large.
{
"error": {
"code": "BadRequest",
"message": "Maximum request length exceeded.",
"innerError": {
"request-id": "3c557cf9-6fb6-4f59-9643-419fbcd081db",
"date": "2018-02-13T00:39:49"
}
}
}
So what is the best way to upload a file to SharePoint using the Microsoft Graph? Unfortunately, it seems that there isn’t a single way to do it. By default, use the resumable upload; but if you are saving to a location that has more than the threshold allows and the file is to be overwritten, use the straight upload method. But then, if that file is over 4MB, you can’t do a thing. UPDATE: Colin Wood brought to my attention the SharePoint REST API should be able to perform the upload, as the file upload limit is 2GB, but I haven’t yet tested this against uploading to a list of more than 5000 items. I do know however using the SharePoint CSOM libraries does not fail in the above situation.
Feature photo by Adam Birkett on Unsplash