3

Documentation:

https://learn.microsoft.com/en-us/graph/api/driveitem-createuploadsession?view=graph-rest-1.0

I am able to get the Drive ID of the sub site using the sites/domain/drives api. Using this and the driveId/root/children I can confirm several folders on this drive.

If I use the API POST /drives/{driveId}/items/{itemId}/createUploadSession replacing the driveId with the drive ID and the itemId with the folder ID I get the following error

{
  "error": {
    "code": "nameAlreadyExists",
    "message": "Cannot create an upload session on a folder",
    "innerError": {
      "request-id": "609e17d5-b3f8-455d-9d0f-4849872d8cfa",
      "date": "2020-02-25T20:31:33"
    }
  }
}

If the folder ID is not the ItemID, what is an ItemID ? How do I specify the folder that I want to save a file to? Or a subfolder or folders? None of the otpions let me use anything other than ItemID

POST /drives/{driveId}/items/{itemId}/createUploadSession
POST /groups/{groupId}/drive/items/{itemId}/createUploadSession
POST /me/drive/items/{itemId}/createUploadSession
POST /sites/{siteId}/drive/items/{itemId}/createUploadSession
POST /users/{userId}/drive/items/{itemId}/createUploadSession 

1 Answers1

8

The request options in your question can only be used to upload a new version of an already existing document by using the id of the document. A folder does not have a document, that can be replace with a newer version. Therefore, it is not possible to create an upload session for a folder. You need to specify the name of the file as a relative path after the folder id in order to create an upload session for a new file in the folder:

https://graph.microsoft.com/v1.0/sites/{siteId}/drives/{driveId}/items/{folderId}:/{fileNameUploadFile}:/createUploadSession

You can also use the folder name instead of the folder id:

https://graph.microsoft.com/v1.0/sites/{siteId}/drives/{driveId}/root:/{folderName}/{fileNameUploadFile}:/createUploadSession
Marc
  • 668
  • 7
  • 15
  • Thanks this is just what I needed. is there anywhere in the documentation it describes this? In the page I'm looking at https://learn.microsoft.com/en-us/graph/api/driveitem-createuploadsession?view=graph-rest-1.0 it only describes `POST`ing into an existing `itemid` (which I presume to be an existing document) – Andy Jun 16 '21 at 09:43
  • 1
    In the link you provided there are examples for using the file name to upload or update large documents in the section about upload operation customization via @microsoft.graph.conflictBehavior if you scroll further down. – Marc Jun 16 '21 at 15:07
  • For language-specific implementation refer - (https://learn.microsoft.com/en-us/graph/sdks/large-file-upload?tabs=java). As per the requirement you want to upload on non-root, I am adding a code snippet for the Java language: Refer complete code from the mentioned URL. The only change would be creating uploadSession graphClient .drives(driveId) .items(parentFolderId) .itemWithPath(fileNameWithExtension) .createUploadSession(uploadParams) .buildRequest() .post(); – Jatish Jun 14 '22 at 07:51