Click here to Skip to main content
15,845,785 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
What i want to achieve is to create a folder(main folder) then create subfolders within the main folder.

I have tried the code below but doesnt seem to work. It doesnt create the main folder at all.

Please help. Any suggestion will be highly appreciated.
Thanks

What I have tried:

function createFolder(){

var folder_path;
var mainFolder ="PQ";
var subfolder = ["ITT Files", "Tender Checklist", "Working Files", "Pre-Clarification Files", "Technical Proposal", "Commercial Porposal","Post Bid Clarification"];

for ( var i = 0, l = subfolder.length; i < l; i++ ) {

folder_path= mainFolder+'/'+subfolder[i];
return $.ajax({
url: "https://XYZ.sharepoint.com/TEST/_api/Web/Folders/add('TenderFilesUpload/"+mainFolder+"/"+subfolder[i]+"')",
type: "POST",
dataType: "json",
headers: {
"X-RequestDigest": formDigest,
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose"
},
success: function(data){
console.log("Folder Created!!!");
},
error: function(data, errCode, errMessage) {
swal( 'Create Folder!', errMessage , 'error'
);console.log(data);console.log(errCode);console.log(errMessage);
}

});

}

}
Posted
Updated 9-Sep-19 21:54pm
Comments
Member 9926685 8-Aug-17 12:24pm    
Can you tell TenderFilesUpload is document library or any other list? I have checked your code with document library. It will work if PQ folder is already exists.

Hi,
Try updating rest call syntax.
JavaScript




var serverRelativeUrlToFolder = "Document Library/"+"FolderName";//For Main folder FolderName would be blank
var FolderName ="FolderName";//New Folder Name that to be created
$.ajax({
	url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetFolderByServerRelativeUrl('" + serverRelativeUrlToFolder + "')/folders",
	method: "POST",
	body: "{ '__metadata':{ 'type': 'SP.Folder' }, 'ServerRelativeUrl':'" + FolderName + "' }",
	async: false,
	headers:
	{
		"accept": "application/json; odata=verbose",
		"content-type": "application/json; odata=verbose",
		"X-RequestDigest": $("#__REQUESTDIGEST").val()
	},
	success: function (data) {
		console.log("Folder Created");
	},
	error: function (data) {
		console.log("Error In Folder Creation" + data.body);
	}
});
 
Share this answer
 
function createFolder(){

var folder_path;
var mainFolder ="FolderNmae";
//var mainFolder ="FolderName/Subfoldername"; //create folder in subfolder
var subfolder = ["ITT Files", "Tender Checklist", "Working Files", "Pre-Clarification Files", "Technical Proposal", "Commercial Porposal","Post Bid Clarification"];

for (i=0;i<subfolder.length;i++) {

folder_path= mainFolder+'/'+subfolder[i];
$.ajax({
url: "siteurl/sites/sitename/_api/Web/Folders/add('libraryname/"+mainFolder+"/"+subfolder[i]+"')",
type: "POST",
dataType: "json",
headers: {
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose"
},
success: function(data){
console.log("Folder Created!!!");
},
error: function(data, errCode, errMessage) {
console.log(errCode);
}
});

}

}
 
Share this answer
 
Comments
CHill60 10-Sep-19 3:15am    
If you are going to resurrect old, answered posts with new solutions please include an explanation about what is different with your code to that already posted, or indeed, to the Original Post. Exactly how this resolves the question is not clear

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900