Create folder inside document library using Rest API in SharePoint Online Office 365

create folder using rest api sharepoint 2013

Here in this post, we will discuss how we can create a folder inside the document library using Rest API in SharePoint online Office 365. The same rest API code will also work as it is in SharePoint 2016 and SharePoint 2013.

In this example, we have taken a textbox and a submit button. The user can give a folder name in the textbox and click on submit. On successful creation of the folder, it will show a successful message.

Here let us put every code inside a script editor web part. So both the HTML and rest API code we will put inside script editor web part inside a web part page.

You can also read my previous posts on:
Create column in list using Rest API in SharePoint Online Office 365

Start a task process workflow action Nintex workflow for office 365 SharePoint Online

Configure On Premise environment for Apps in SharePoint 2016

Below is the HTML code:

<div>

<strong>Enter Folder Name:</strong>

<input type=”text” id=”txtFolderName” />

<input type=”button” id=”btnSubmit” value=”Submit” />

</div>

<div id=”divResults”></div>

Rest API code:
In the below code, we will create folder inside the “Documents” document library. In the metadata we are passing through ServerRelativeUrl where we want to create the document library.

<script src=”https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js”></script>

<script>

$(function () {

bindButtonClick();

});

function bindButtonClick() {

$(“#btnSubmit”).on(“click”, function () {

createFolder();

});

}

function createFolder() {

var folderName = $(“#txtFolderName”).val();

var siteUrl = _spPageContextInfo.webAbsoluteUrl;

var fullUrl = siteUrl + “/_api/web/folders”;

$.ajax({

url: fullUrl,

type: “POST”,

data: JSON.stringify({

‘__metadata’: { ‘type’: ‘SP.Folder’ },

‘ServerRelativeUrl’: ‘Shared Documents/’ + folderName

}),

headers: {

“accept”: “application/json;odata=verbose”,

“content-type”: “application/json;odata=verbose”,

“X-RequestDigest”: $(“#__REQUESTDIGEST”).val()

},

success: onQuerySucceeded,

error: onQueryFailed

});

}

function onQuerySucceeded() {

$(“#divResults”).html(“Folder created successfully !!!”);

}

function onQueryFailed() {

alert(‘Error!’);

}

</script>

Once you will Save the code, give a name for the folder and click on Submit. It will show a successful message like below:

SharePoint online create folder using rest api
SharePoint online create folder using rest api

Now if you will go to the Documents document library, you can see the folder got created like below:

create folder using rest api sharepoint 2013
create folder using rest api sharepoint 2013

Hope this will be helpful.


You May Also like the Following SharePoint Online Tutorials:

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →