sGambolati
sGambolati

Reputation: 782

BadRequest when try Create folder via REST

I've downloaded a exampled that show the files in the "Shared with everyone" folder in my OneDrive for Bussiness. It's work fine! But, when I try to create a Folder or File (without content) like this documentation the response became with a BadRequest .

The request goes like:

        string requestUrl = String.Format(CultureInfo.InvariantCulture, "{0}/files", serviceInfo.ApiEndpoint);

        // Prepare the HTTP request:
        using (HttpClient client = new HttpClient())
        {
            Func<HttpRequestMessage> requestCreator = () =>
            {
                HttpRequestMessage request = new HttpRequestMessage( HttpMethod.Post, requestUrl);
                request.Headers.Add("Accept", "application/json;odata.metadata=full");                  
                request.Content = new StringContent(@"{'__metadata':{'type':'MS.FileServices.Folder'},Name:'TestFolder'}");
                request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                return request;
            };

}

And the response is a BadRequest. I think that my problem is in the "__metadata"'s json value. It´s is correct? Where can I find a working example implementing this operations?

Thanks in advance!

EDIT: Changing the API Endpoint from "/_api/web/getfolderbyserverrelativeurl('Documents')/files" to "_api/files" the error became to: "The property '__metadata' does not exist on type 'MS.FileServices.FileSystemItem'. Make sure to only use property names that are defined by the type."

I´m think I foward in this. But, I still continue with problems.

Upvotes: 1

Views: 1476

Answers (2)

Ajay Kumar
Ajay Kumar

Reputation: 5233

I am trying to below way to create a folder in SP and it's working for me. Hope it will work for you as well.

Create a folder using SharePoint Web API -

POST https://<Domain>.sharepoint.com/_api/web/folders
Accept: "application/json;odata=verbose"
Content-Type: "application/json"

{
  "ServerRelativeUrl": "/Shared Documents/<Folder-Name>"
}

Upvotes: 0

stackunderflow
stackunderflow

Reputation: 3873

I am not sure if this can be of any help to you, as this pertains to oneDrive and not oneDrive for business. Also the documentations are confusing :)

according to the documentation the request should be as follow:

POST https://apis.live.net/v5.0/me/skydrive

Authorization: Bearer ACCESS_TOKEN
Content-Type: application/json

{
    "name": "My example folder"
}

if you can see that in the header there is authorization access token

I don't see that you sent to the server any access token. and that is why you had a bad request.

Upvotes: 1

Related Questions