I am attempting to use JSOM to add a folder to a pre-created folder within a specified library. In the code below, I have checked that all of the variables are functioning as expected - meaning the library, the url, the arguments used as leaf names. However the query fails every time and I get the error "Value does not fall within specified range". The strange part is that if I attempt to add multiple folders, the first one is added, and the rest are dropped. Any help will be much appreciated.
function storeFolderTest() {
var curLibrary = empLib;
for (var i = 0; i < arguments.length; i++) {
var folderURL = hostURL + "/" + "Employee Files" + "/" + "testFolder";
var itemCreateInfo_subFolder = new SP.ListItemCreationInformation();
itemCreateInfo_subFolder.set_underlyingObjectType(SP.FileSystemObjectType.folder);
itemCreateInfo_subFolder.set_folderUrl(folderURL);
itemCreateInfo_subFolder.set_leafName(arguments[i]);
var oListItem2 = curLibrary.addItem(itemCreateInfo_subFolder);
oListItem2.update();
}
context.load(oListItem2);
context.executeQueryAsync(onQuerySucceeded, onQueryFailed);
function onQuerySucceeded() {
alert('folder uploaded');
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
}