Can I create a file to subdirectory on OneDrive for Business with REST API?

395 Views Asked by At

I'm new to the OneDrive for Business REST service and I have no problems creating a file in the root user directory. Unfortunately, I'm trying to create a file in a shared subdirectory with the following code:

    post https://foo-my.sharepoint.com/_api/v1.0/me/files
/01PRDMT2S3RER67TKPWRELGF6PPPAUCVZT  HTTP/1.1
    OData-Version: 4.0;NetFx
    OData-MaxVersion: 4.0;NetFx
    Accept: application/json;odata.metadata=minimal
    Accept-Charset: UTF-8
    User-Agent: Microsoft ADO.NET Data Services
    Authorization: Bearer .........
    X-ClientService-ClientTag: Office 365 API Tools 1.1.0612
    Host: foo-my.sharepoint.com
    Content-Length: 65
    Expect: 100-continue

    {"@odata.type":"#Microsoft.FileServices.File","name":"demo1.txt"}

Unfortunately, I continue to get the following error:

98 {"error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":"The parameter @odata.type does not exist in method GetById."}} 0

Is it possible to create a file in a sub directory?

1

There are 1 best solutions below

0
On

try to use address like https://foo-my.sharepoint.com/_api/v1.0/me/files/{ParentId}/children

also, in the data you pass, specify a "type" member:

{ name: "myfile.txt", type: "File" }