Create folder inside document library using JavaScript object model jsom in SharePoint online office 365

Create folder inside document library using JavaScript object model jsom in SharePoint online office 365

In this post, we will discuss how we can create a folder inside a document library using JavaScript object model(jsom) in SharePoint online Office 365. We have also discussed how we can create a folder inside a document library using Rest API 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.

Also you can read some useful posts on:
Create SharePoint Site using Rest API in SharePoint Online Office 365

App Step in SharePoint 2013 Designer Workflow

Insert item to SharePoint list using JavaScript Object Model JSOM in SharePoint Online

HTML Code:
Below is the full html code where we have take a 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:

<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 object model jsom in SharePoint online office 365
Create folder inside document library using JavaScript object model jsom in SharePoint online office 365

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

Create folder inside document library using JavaScript object model jsom in SharePoint online office 365
Create folder inside document library using JavaScript object model jsom in SharePoint online office 365


Hope this will be helpful.

Check out Best Alternative to InfoPath -> Try Now

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 →