SharePoint rest api create folder | SharePoint create folder programmatically using CSOM

In this SharePoint Rest API tutorial, we will discuss on SharePoint rest api create folder. It will help you:

  • How to create folder in SharePoint document library using rest api
  • create a folder inside a SharePoint document library using the JavaScript object model
  • Create folder SharePoint rest api
  • Create folder in SharePoint document library using CSOM
  • SharePoint 2013 create folder programmatically
  • SharePoint online create folder programmatically
  • How to delete a folder in sharepoint using rest api

The same rest API code we can use to create a folder inside a document library in SharePoint 2016 and SharePoint 2013.

SharePoint rest api create folder

In this example, we have created an HTML form which has 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 inside the document library, it will show a successful message.

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

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 a 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 rest api create folder
sharepoint rest api create folder

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

SharePoint rest api create folder inside document library
sharepoint create folder rest api

SharePoint rest api create folder inside document library

Let us see another example, how to create a folder inside the document library using rest api in SharePoint Online/2013/2016. We will also discuss how to delete the folder from the document library in SharePoint 2013/2016/Online using rest api.

If you are new to rest api, then check out SharePoint rest api tutorial and examples.

Below is the rest api code to create a folder inside a document library in SharePoint. The code we can insert inside a script editor web part or content editor web part in a SharePoint web part page.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(function(){
$("#btnClick").click(function(){
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/folders";
$.ajax({
url: requestUri,
type: "POST",
data:JSON.stringify({'__metadata': { 'type': 'SP.Folder' }, 'ServerRelativeUrl': 'Shared Documents/Folder1'}),
headers: {
"accept":"application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest":$("#_REQUESTDIGEST").val()
},
success: onSuccess,
error: onError
});

function onSuccess(data) {
alert('Folder created successfully');
}

function onError(error) {
alert(JSON.stringify(error));
}

});

});

</script>

<input type="button" id="btnClick" value="Create Folder Inside Document Library"/>

The above rest api code will create a folder inside Shared Documents document library in SharePoint.

Delete folder from SharePoint Document library using Rest API

Below is the rest api code to delete a folder from a document library in SharePoint. The code we can insert inside a script editor web part or content editor web part in a SharePoint web part page.

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

<script>
$(function(){
$("#btnClick").click(function(){
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetFolderByServerRelativeUrl('Shared Documents/Folder1')";
$.ajax({
url: requestUri,
type: "POST",
headers: {
"X-HTTP-Method":"DELETE"
},
success: onSuccess,
error: onError
});

function onSuccess(data) {
alert('Folder deleted successfully');
}

function onError(error) {
alert(JSON.stringify(error));
}
});

});

</script>

<input type="button" id="btnClick" value="Delete Folder From Document Library"/>

The above rest api code will delete the folder name as Folder1 from Shared Documents document library in SharePoint.

Create a folder using JavaScript in SharePoint

Now, we will see how to create a folder inside a document library using the JavaScript object model(jsom) in SharePoint Online Office 365.

Here we will take a textbox and a button where a user can give a name for the folder and click on Create Folder button which will create the folder inside the document library. Both the HTML and javascript object model code, let us put inside a script editor web part which will be inside a web part page.

HTML Code:

Below is the full HTML code where we have to take an HTML textbox and an HTML button.

<div>
<strong>Enter FolderName:</strong><br />
<input type="text" id="txtFolderName" /><br />
<input type="button" id="btnSubmit" value="Create Folder" />
</div>
<div id="divResults"></div>

JSOM Code:

Below is the full jsom code. Here also we have given reference to the jQuery min file because we have used jQuery to bind the button click even as well as we are also retrieving the textbox value using jQuery like below:

var folderName = $("#txtFolderName").val();

Here we are creating the folder inside “Documents” document library, if you want to give any other document library then you can just replace the document library name is the below line.

var oList = oWebsite.get_lists().getByTitle("Documents");

Below is the full jsom code to create a folder inside a SharePoint 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 clientContext = new SP.ClientContext();
var oWebsite = clientContext.get_web();
var oList = oWebsite.get_lists().getByTitle("Documents");
var folderCreateInfo = new SP.ListItemCreationInformation();
folderCreateInfo.set_underlyingObjectType(SP.FileSystemObjectType.folder);
folderCreateInfo.set_leafName(folderName);
this.oListItem = oList.addItem(folderCreateInfo);
this.oListItem.update();
clientContext.load(oList);
clientContext.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceeded),Function.createDelegate(this, this.onQueryFailed)
);
}

function onQuerySucceeded() {
$("#divResults").html("Folder successfully created!");
}

function onQueryFailed(sender, args) {
alert(‘Request failed. ‘ + args.get_message() +
‘\n’ + args.get_stackTrace());
}
</script>

Once you save the page and refresh, you can see the HTML controls. Give a name for the folder and then click on Create Folder. It will create the folder and give a successful message like below:

create folder inside document library using javascript
sharepoint create folder programmatically

You can also navigate to the document library, where you can see the folder got created like below.

create folder inside sharepoint document library using jsom
sharepoint 2013 create folder programmatically

This is how we can create a folder inside a document library using JavaScript object model (jsom) in SharePoint Online.

Create folder in SharePoint document library using CSOM

This SharePoint tutorial explains, how to create folders and subfolders inside a document library in SharePoint Online using (csom) .Net managed client object model code.

Here I have a SharePoint document library and by using the client-side code we will create a folder and within that folder, we will also create another subfolder using C#.

Create a folder in SharePoint document library using csom

In this demo we will check inside a windows application using visual studio 2017, you can also create a console application in c#.net.

  • Microsoft.SharePoint.Client.dll
  • Microsoft.SharePoint.Client.Runtime.dlls

Below is the full code.

using (ClientContext clientContext = new ClientContext("https://onlysharepoint2013.sharepoint.com/sites/Bhawana/"))
{
SecureString passWord = new SecureString();
foreach (char c in "********".ToCharArray()) passWord.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials("[email protected]", passWord);
var web = clientContext.Web;
var lst = web.Lists.GetByTitle("MyDemoDocuments");
var fld1 = lst.RootFolder.Folders.Add("FirstLevel1");
var fld2 = fld1.Folders.Add("SecondLevel2");
fld1.Update();
fld2.Update();
clientContext.ExecuteQuery();
}

After running the code it will create a folder and subfolder inside the SharePoint document library.

This is how we can create a folder and subfolders in the SharePoint document library using csom.

You may like following SharePoint rest api tutorials:

I hope this SharePoint tutorial helped:

  • sharepoint rest api create folder
  • create folder sharepoint rest api
  • How to create folder in sharepoint document library using rest api
  • create folder in sharepoint document library using rest api
  • create folder in sharepoint list using rest api
  • create new folder in sharepoint 2013 list using rest api
  • sharepoint csom create folder
  • sharepoint create folder programmatically
  • sharepoint online create folder programmatically
  • sharepoint 2013 create folder in document library programmatically
  • sharepoint document library create folder programmatically
Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

  • 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.

  • >