AnsweredAssumed Answered

Using the Alfresco NodeJS API to create a folder path

Question asked by pedwards99 on Sep 13, 2018
Latest reply on Sep 21, 2018 by pedwards99

Hi there,


Has anyone ever managed to create a folder structure in Alfresco using the Javascript API and NodeJS?


I think my problem is a lack of understanding of Node, rather than Alfresco, but here's what I'm trying to achieve:


Given a folder path as a string e.g. /Cities/London/Stations/Euston I want to create that folder path at a give root inside Alfresco.


Using Java, one solution would be to split the folder path string at '/' then loop through each folder name creating a folder then using it as the parent for the next folder. This works because each folder is created before the code moves on to create the next folder.


The non-blocking nature of Node means if I simply put a for loop around the API call then it will fail because you cannot  guarantee that the parent folders will have been created by the time Alfresco tries to create the subfolder. 


alfrescoJsApi.nodes.createFolder(folderName, parentFolder).then(function (data) {
   console.log('Folder ' + folderName + ' created');
   }, function (error) {
      console.log('Error creating folder ' + folderName + ' in ' + parentFolder + error);


Any ideas on how this should be done?


Is it best to write my own webscript which creates the whole folder path then call that once from the NodeJS code?


Many thanks