When you add a folder in SharePoint using the Web UI, it actually adds two things: the folder and a list item. It's the list item that shows up in the List View.
Also, there are two names. The name of the list item and the name of the folder. You set the name of the list item by setting the Title field value and you set the name of the folder by setting the FileLeafRef field value.
When you add a folder using the REST API, it just adds the folder.
I've gotten folder creation to work but it's sort of a hack. I add the list item and set the content type to folder. This adds the list item and the folder. The problem is that trying to set the name of the folder on creation does not work. You need to create the list item and then set the name of the folder as a second step.
I've reported this as an issue on the Office 365 Developer User Voice: Improve Folder support in the REST API
function createFolder() {
UpdateFormDigest(_spPageContextInfo.webServerRelativeUrl, _spFormDigestRefreshInterval);
var appUrl = GetUrlKeyValue("SPAppWebUrl");
var hostUrl = GetUrlKeyValue("SPHostUrl");
var url = appUrl + "/_api/SP.AppContextSite(@target)/Web/Lists/getByTitle('Demo')/Items?@target='" + hostUrl + "'";
var call = createFolderInternal().then(renameFolder);
call.done(function (data, textStatus, jqXHR) {
var message = jQuery("#message");
message.text("Folder added");
});
call.fail(failHandler);
function createFolderInternal() {
var url = appUrl + "/_api/SP.AppContextSite(@target)/Web/Lists/getByTitle('Demo')/Items?@target='" + hostUrl + "'";
var call = jQuery.ajax({
url: url,
type: "POST",
data: JSON.stringify({
"__metadata": { type: "SP.Data.DemoListItem" },
Title: "Test Folder",
FileLeafRef: "Test Folder", // No effect here
FileSystemObjectType: SP.FileSystemObjectType.folder,
ContentTypeId: "0x0120"
}),
headers: {
Accept: "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose",
"X-RequestDigest": jQuery("#__REQUESTDIGEST").val()
}
});
return call;
}
function renameFolder(data) {
var url = appUrl + "/_api/SP.AppContextSite(@target)/Web/Lists/getByTitle('Demo')/Items('" + data.d.Id + "')?@target='" + hostUrl + "'";
var call = jQuery.ajax({
url: url,
type: "POST",
data: JSON.stringify({
"__metadata": { type: "SP.Data.DemoListItem" },
Title: "Test Folder",
FileLeafRef: "Test Folder"
}),
headers: {
Accept: "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose",
"X-RequestDigest": jQuery("#__REQUESTDIGEST").val(),
"IF-MATCH": "*",
"X-Http-Method": "PATCH"
}
});
}
}