According to Working with folders and files with REST the following example demonstrates how to create a Folder:
url: http://site url/_api/web/folders
method: POST
body: { '__metadata': { 'type': 'SP.Folder' }, 'ServerRelativeUrl': '/document library relative url/folder name'}
Headers:
Authorization: "Bearer " + accessToken
X-RequestDigest: form digest value
accept: "application/json;odata=verbose"
content-type: "application/json;odata=verbose"
content-length:length of post body
JavaScript example (jQuery)
function createFolder(webUrl,folderUrl) {
var folderPayload = { '__metadata': { 'type': 'SP.Folder' }, 'ServerRelativeUrl': folderUrl};
return $.ajax({
url: webUrl + "/_api/web/folders",
type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify(folderPayload),
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
}
});
}
Usage
//create a folder named '2015' in Documents library
createFolder(_spPageContextInfo.webAbsoluteUrl,'Documents/2015')
.done(function(data)
{
var folder = data.d;
console.log('Folder ' + folder.Name + ' has been created successfully');
})
.fail(
function(error){
console.log(JSON.stringify(error));
});
JavaScript example (datajs)
The same example using datajs library:
var folderPayload = { '__metadata': { 'type': 'SP.Folder' }, 'ServerRelativeUrl': 'Documents/2016'};
OData.request(
{
requestUri: _spPageContextInfo.webAbsoluteUrl + "/_api/web/folders",
method: "POST",
headers: { "X-RequestDigest": $("#__REQUESTDIGEST").val(), "DataServiceVersion": "3.0" },
data: folderPayload
},
function(folder)
{
console.log('Folder ' + folder.Name + ' has been created successfully');
},
function(error){
console.log(JSON.stringify(error));
});