1

My aim is to create a folder with % in folder name if it doesn't exist in SP Server-2019 using REST.

 POST http://<site>/_api/web/folders

{
  "__metadata": {
    "type": "SP.Folder"
  },
  "ServerRelativeUrl": "/SP 2019/Folder%"
}

But this is creating a Folder%25 instead of Folder%. So, after reading this article, i am using:

POST http://<site>/_api/web/folders/AddUsingPath(decodedurl='Path')

Note: This successfully creates a folder with name '%' if it does not exist.

The issue i am running into is the api returns 400 Bad Request in two cases: a) The folder already exists. b) The URL is malformed.

I need to differentiate between the two. Or if folder creation if possible using some other api call, that would be helpful!

Ref: Stackoverflow

3 Answers 3

0

You could try using encodeURI and encodeURIComponent, sample usage here. What it does is it will encode the uri to satisfy the server request with the right url format.

Hope the concept helps, Happy Coding!

1
  • I have tried sending encoded url in the path. But it didnt help.
    – Shubham
    Apr 16, 2020 at 8:56
0

You will need to specify the folder using %25 actually. This will be decoded into %!

POST http://<site>/_api/web/folders/AddUsingPath(decodedurl='Folder%25')

Note: This will create the folder in the root of the site. You probably want to include a library in the url.
Here is my test that created a folder successfully:

    let ctx = this.props.context;
    let options: ISPHttpClientOptions = {};
    let folder = "Shared%20Documents/Folder%25";
    let url = `/sites/Playground/_api/web/Folders/AddUsingPath(decodedurl='${folder}')`; 
    ctx.spHttpClient.post(url, SPHttpClient.configurations.v1, options).then((res:SPHttpClientResponse) => {
      res.text().then(text => {
        console.log(text);
      });
    }).catch(err => {
      console.log(err);
    });

enter image description here

1
  • I think i haven't the question clearly. I am doing the steps as you suggested. My issue is that when the folder is already present, it gives 400 Bad Request Error. Which is a client side error. The earlier call (the one with metadata Ref: Question above) returns 201 Created even when the folder exists.
    – Shubham
    Apr 16, 2020 at 8:36
0

You could check if the folder already exists before creating folder.

http://site url/_api/web/GetFolderByServerRelativePath(decodedUrl='folder name')

If the request returns a value, it means that the folder already exists.

For SharePoint Online ,you could try to use Microsoft Graph API to create folder. Here is the document for your reference:

https://docs.microsoft.com/en-us/graph/api/driveitem-post-children?view=graph-rest-beta&tabs=http

https://graph.microsoft.com/v1.0/sites/{site-id}/lists/{list-id}/drive/root:/{path}:/children
https://graph.microsoft.com/v1.0/sites/{site-id}/lists/{list-id}/drive/root/children
3
  • Hi! Yes that would be the last option. I don't really want to double the number of calls made.
    – Shubham
    Apr 16, 2020 at 9:47
  • The endpoint in your referenced issue ‘POST http://<site>/_api/web/folders’ will not create a new folder when the folder has existed.But as you know,this endpoint requires you to deal with special characters.
    – Amos
    Apr 17, 2020 at 6:09
  • Yes, was just searching for a way that tells me clearly that the folder already exists instead of a 400 Bad Req error.
    – Shubham
    Apr 17, 2020 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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