2

Related to question Creating Folder using REST and JavaScript, I used 3 approaches to create folder with REST API (native C++ casablanca lib):

request.set_method(methods::POST);
request.headers().add(U("X-RequestDigest"), my_digest);
request.headers().add(U("content-type"), U("application/json; odata=verbose"));
  1. Works OK

    request.set_request_uri(web::uri::encode_uri(U("/_api/Web/Folders/add('/Shared Documents/new folder created')")));
    
  2. HTTP 400 Bad request (example from MSDN)

    request.set_request_uri(web::uri::encode_uri(U("/_api/Web/GetFolderByServerRelativeUrl('/Shared Documents')/Folders")));
    request.set_body(U("{ '__metadata':{ 'type': 'SP.Folder' }, 'ServerRelativeUrl':'new folder created' }"));
    
  3. HTTP 400 Bad request (example from MSDN)

    request.set_request_uri(web::uri::encode_uri(U("/_api/Web/Folders/")));
    request.set_body(U("{ '__metadata':{ 'type': 'SP.Folder' }, 'ServerRelativeUrl':'Shared Documents/new folder created' }"));
    

Are there any ideas why only #1 works? Can anyone confirm that it works from another environments like JavaScript?

  • tried set Header to request ? – b1c10 Dec 12 '14 at 17:43
  • Yes, headers are set - I wrote that #1 works. It wouldn't without headers. But anyway - I will add those code to question. – andrii Dec 13 '14 at 8:24
3

SP.Folder.serverRelativeUrl property accepts the following formats:

  • /Site_Name/SubSite_Name/Library_Name/File_Name
  • Library_Name/File_Name

Assuming the following structure:

/ (Root site)

   News sub site

     Documents library

the following examples demonstrate how to create a Folder:

Case 2

Example 1

Url: http://<server>/news/_api/Web/GetFolderByServerRelativeUrl('/news/Shared Documents')/Folders

Body: { '__metadata': { 'type': 'SP.Folder' }, 'ServerRelativeUrl': '/news/Shared Documents/New Folder'};

Example 2

Url: http://<server>/news/_api/Web/GetFolderByServerRelativeUrl('Shared Documents')/Folders

Body: { '__metadata': { 'type': 'SP.Folder' }, 'ServerRelativeUrl': 'Shared Documents/New Folder'};

Case 3

Example 1

Url: http://<server>/news/_api/web/folders/

Body: { '__metadata': { 'type': 'SP.Folder' }, 'ServerRelativeUrl': '/news/Shared Documents/New Folder'};

Example 2

Url: http://<server>/news/_api/web/folders/

Body: { '__metadata': { 'type': 'SP.Folder' }, 'ServerRelativeUrl': 'Shared Documents/New Folder'};

0

As far as I see the value of the ServerRelativeUrl parameter in #2 and #3 is not correct. See the examples on MSDN you reffered to. In #2 it is simply the folder name, in #3 the leading slash is missing. Should be '/Shared Documents/new folder created' instead, the same value yuo use in the working #1.

I suggest you to use Fiddler or another network sniffing tool to see the possibe error details returned by the server over the general HTTP 400 Bad Request response status message.

  • You are right about #3. Regarding #2 - after server restart it occasionally stared working! I guess that it is doesn't matter how you call folders creating - from root Folders collection and pass long path to new folder, or just pass a new folder name but for Folders collection of concrete parent folder – andrii Dec 13 '14 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.