davidvera
davidvera

Reputation: 1489

Sharepoint : create a new file in a specific drive with graph API

In order to create a new file in a drive (a simple text file with some content), I try to call an API with MS Graph.

PUT /sites/{site-id}/drive/items/{parent-id}:/{filename}:/content

The drive where I want to put the file is name "myFolder" and has this driveId :

b!e5bDF5eRbU2Y7P3gHeS-0F1abPhpWXdOvSUViyWpqX876IbeJvIPS5-tf--QTQiz

This drive is in the root site.

Here :

If I had to create this file in a subsite it would

So theoretically my URL should be the following :

https://graph.microsoft.com/v1.0/sites/root/drives/b!e5bDF5eRbU2Y7P3gHeS-0F1abPhpWXdOvSUViyWpqX876IbeJvIPS5-tf--QTQiz:/text.txt:/content

Here :

I also define the request header Content-Type as text/plain My issue is that I retrieve an error message :

{
  "error": {
    "code": "BadRequest",
    "message": "Resource not found for the segment 'content'.",
    "innerError": {
      "date": "2020-06-26T14:19:11",
      "request-id": "812b7ee0-3ecb-4d41-a8c5-59419b086f51"
    }
  }
}

Upvotes: 2

Views: 6110

Answers (2)

Baker_Kong
Baker_Kong

Reputation: 1889

You can directly create file in subsite library using below endpoints:

https://graph.microsoft.com/v1.0/sites/{sub-siteid}/drives/{driveid}/root:/FolderA/FileB.txt:/content

Reference doc: https://learn.microsoft.com/en-us/graph/api/driveitem-put-content?view=graph-rest-1.0&tabs=http#example-upload-a-new-file

And if you want to get subsite id, you can refer to :

Demo request: enter image description here

Upvotes: 1

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59338

This is expected error since for addressing a file within a drive (which is document library in SharePoint) a folder container needs to be provided:

So, instead of query

https://graph.microsoft.com/v1.0/sites/root/drives/{drive-id}:/{file-name}:/content

it should be

https://graph.microsoft.com/v1.0/sites/root/drives/{drive-id}/root:/{folder-path}/{file-name}:/content

where folder-path corresponds to Test folder in your case

References

Addressing resources in a drive on OneDrive

Upvotes: 4

Related Questions