How to Create a Folder in SharePoint using REST API?

Are you trying to create a folder in a SharePoint document library using the REST API? You will get the complete code here. In this tutorial, I will explain how to create a folder in SharePoint using the REST API.

Create a Folder in SharePoint using REST API

To create a folder in a SharePoint document library, you’ll use the following REST endpoint:

POST https://<site-url>/_api/web/folders

You need to provide the folder path in the request body.

Suppose your site URL is https://contoso.sharepoint.com/sites/Marketing. The REST endpoint becomes:

https://contoso.sharepoint.com/sites/Marketing/_api/web/folders

The request body must be in JSON format and include the ServerRelativeUrl property, which is the relative path to your new folder:

{
  "__metadata": { "type": "SP.Folder" },
  "ServerRelativeUrl": "/sites/Marketing/Shared Documents/ProjectDocs"
}

You need the following headers:

  • Accept: application/json;odata=verbose
  • Content-Type: application/json;odata=verbose
  • X-RequestDigest: Form digest value for authentication (if using JavaScript or REST clients within SharePoint)

Here is the code:

const siteUrl = "https://contoso.sharepoint.com/sites/Marketing";
const folderName = "ProjectDocs";
const library = "Shared Documents";

fetch(`${siteUrl}/_api/web/folders`, {
  method: "POST",
  headers: {
    "Accept": "application/json;odata=verbose",
    "Content-Type": "application/json;odata=verbose",
    "X-RequestDigest": document.getElementById("__REQUESTDIGEST").value
  },
  body: JSON.stringify({
    "__metadata": { "type": "SP.Folder" },
    "ServerRelativeUrl": `/sites/Marketing/${library}/${folderName}`
  })
})
.then(response => response.json())
.then(data => console.log("Folder created!", data))
.catch(error => console.error("Error:", error));

Check out SharePoint Rest API Tutorial and Examples

Create a SharePoint Folder using the REST API

Now, let me show you an example of creating a folder in a SharePoint document library using the REST API.

For example, we will create a text box with a create button. Here, the text box will accept the folder name, and clicking on the create button will trigger the creation of a folder in the Finance Document Library on the SharePoint site using REST API requests.

Below is the complete REST API code you can add in a script editor web part.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
    function createFolder() {
        var folderName = $("#txtFolderName").val();
        var siteUrl = _spPageContextInfo.webAbsoluteUrl;
        var folderUrl = siteUrl + "/_api/web/getfolderbyserverrelativeurl('Finance')/folders/add('" + folderName + "')";

        $.ajax({
            url: folderUrl,
            type: "POST",
            headers: {
                "accept": "application/json;odata=verbose",
                "content-type": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val()
            },
            success: function () {
                alert("Folder created successfully!");
            },
            error: function (error) {
                alert("Error creating folder: " + JSON.stringify(error));
            }
        });
    }

    $(function () {
        $("#btnSubmit").on("click", function () {
            createFolder();
        });
    });
</script>

<label for="txtFolderName">Folder Name:</label>
<input type="text" id="txtFolderName">
<button id="btnSubmit">Create Folder</button>

Now you can provide the folder name and click the Create button to create the Folder in the Finance Document library in the SharePoint site.

sharepoint rest api create folder

Now you can see the Policies folder get created in the SharePoint library Finance in the SharePoint site using the SharePoint Rest Api.

sharepoint api create folder

This is how to create a folder in SharePoint using the REST API.

Sometimes, you may need to check if the folder exists; if it does, you might not need to create it. Otherwise, you can create it. For this, you can use the code below.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
    function checkFolder() {
        var folderName = $("#txtFolderName").val();
        var siteUrl = _spPageContextInfo.webAbsoluteUrl;
        var folderPath = "Finance/" + folderName;
        var url = siteUrl + "/_api/web/GetFolderByServerRelativeUrl('" + folderPath + "')/exists";

        $.ajax({
            url: url,
            type: "GET",
            headers: {
                "accept": "application/json;odata=verbose",
            },
            success: function (data) {
                if (data.d.Exists) {
                    alert("Folder is already available");

                } else {
                    createFolder(folderName);
                }
            },
            error: function (error) {
                alert("Error checking folder existence: " + JSON.stringify(error));
            }
        });
    }

    function createFolder(folderName) {
        var siteUrl = _spPageContextInfo.webAbsoluteUrl;
        var folderUrl = siteUrl + "/_api/web/lists/getbytitle('Finance')/rootfolder/folders/add('"+ folderName +"')";

        $.ajax({
            url: folderUrl,
            type: "POST",
            headers: {
                "accept": "application/json;odata=verbose",
                "content-type": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val()
            },
            success: function () {
                alert("Folder created successfully!");
            },
            error: function (error) {
                alert("Error creating folder: " + JSON.stringify(error));
            }
        });
    }

    $(function () {
        $("#btnSubmit").on("click", function (event) {
            event.preventDefault();
            checkAndCreateFolder();
        });
    });
</script>

<label for="txtFolderName">Folder Name:</label>
<input type="text" id="txtFolderName">
<button id="btnSubmit">Create Folder</button>

Once you save the code in the script editor on the SharePoint site, after providing the folder name, click the Create button. If it is not available, the folder will be created. If the folder exists, it will notify you that the folder is already available.

In this tutorial, we learned how to create a folder in a SharePoint document library using REST API. Do let me know if you still have any questions.

You may also like the following tutorials:

  • Hi, Can you guide me on how to do this from a native application like a website with only html pages/aspx pages ( application is outside of sharepoint context ) to create a list item/ folder in a library in a sharepoint online site.

  • >

    Build a High-Performance Project Management Site in SharePoint Online

    User registration Power Apps canvas app

    DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

    Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App

    Power Platform Tutorial FREE PDF Download

    FREE Power Platform Tutorial PDF

    Download 135 Pages FREE PDF on Microsoft Power Platform Tutorial. Learn Now…