Create Folder in a simple SP.List using REST API

When you create a folder using the Web UI, SharePoint creates two things:

  1. Creates a folder
  2. Creates a list item

By the way, it’s the list item that you see on the Web UI List View.

So, If you need to create a folder in a List, firstly create a folder based on the ContentTypeId. The ContentTypeIdfor a Folder is 0x0120. Note that you only have the Title available at this level.
Then take the created folder and update it with Title and FileLeafRef. If you have additional columns, update them as well.
If you omit this step, your folder will be created with the specific name, but if you click on the item, you will get the ID as the folder name.

Here is my snipped which I use:

var folderName =”myNewFolder”;
var listName=”myList”;
var call = addNewItemAsFolder(folderName, listName);

function addNewItemAsFolder(folderName, listName) {
var itemType=getItemTypeForListName(listName);
var data= {

“__metadata”: { “type”: itemType},
“Title”: folderName,
“ContentTypeId”:”0x0120″ //ContentType for Folder

};

var url= _spPageContextInfo.webAbsoluteUrl+”/_api/Web/Lists/GetByTitle(‘”+listName+”‘)/Items”;

var call = $.ajax({

url: url,
type: “POST”,
headers: {

“accept”: “application/json;odata=verbose”,
“X-RequestDigest”: $(“#__REQUESTDIGEST”).val(),
“content-Type”: “application/json;odata=verbose”

},
data: JSON.stringify(data),
success: function (data) {

//Update the Folder Items
jQuery.ajax({
url: url+”(‘” + data.d.Id + “‘)”,
type: “POST”,

data: JSON.stringify({

“__metadata”: { type: itemType },
Title: folderName,
FileLeafRef: folderName,
myColumn01:  “this is my additional column”,
myColumn02Name: “this is my additional column”

}),

headers: {

Accept: “application/json;odata=verbose”,
“Content-Type”: “application/json;odata=verbose”,
“X-RequestDigest”: jQuery(“#__REQUESTDIGEST”).val(),
“IF-MATCH”: “*”,
“X-Http-Method”: “PATCH”

}

});

},
error: function (error) {
alert(JSON.stringify(error));
}
});
return call;
}

// Get List Item Type metadata
function getItemTypeForListName(name) {
return “SP.Data.” + name.charAt(0).toUpperCase() + name.split(” “).join(“”).slice(1) + “ListItem”;
}