Using the Alfresco NodeJS API to create a folder path

cancel
Showing results for 
Search instead for 
Did you mean: 
Partner

Using the Alfresco NodeJS API to create a folder path

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

Paul

2 Replies
Professional

Re: Using the Alfresco NodeJS API to create a folder path

The Alfresco JS API actually accepts a path as the parent folder argument and will create each element in the path if it does not exist. Here is an example:

var AlfrescoApi = require('alfresco-js-api-node');

function createFolder(folder, parentFolder, callback) {
  this.alfrescoJsApi = new AlfrescoApi({hostEcm:'http://alfresco.local:8080'});
  this.alfrescoJsApi.login('admin', 'admin').then(function (data) {
      console.log('API called successfully Login ticket:' + data);
      this.alfrescoJsApi.nodes.createFolder(folder, parentFolder).then(function(data) {
        console.log("Created folder");
        return callback(data);
      })
  }, function (error) {
      console.error(error);
  });
}

createFolder("testjs", 'some/path', function(data) {
    console.log("Done creating : " + data.entry.name + " (" + data.entry.id + ")");
})

I didn't have to set up the createFolder function, I could have just done lines 4 through 13 but I somehow find this easier to read.

Running this with node will create a folder named "testjs" under the root under some/path.

To answer your original question, if the path argument wasn't supported, and if you instead had to pass in a node reference or something like that for the parent, you could do the split as you suggest and then recursively call create folder.

Partner

Re: Using the Alfresco NodeJS API to create a folder path

Fantastic !

Thanks Jeff, I didn't realise you could use a folder path as the argument. That makes lief a lot easier :-)