cancel
Showing results for 
Search instead for 
Did you mean: 

Javascript to create subfolder triggered by rule

gmce
Confirmed Champ
Confirmed Champ
Hallo everybody, i am just a noob in javascript development.

I need to create some specific subfolders when i create a folder, so i applyed a rule to the parent folder that execute a script when a folder is created.

The script is this:


var subfolder1 = space.childByNamePath("subfolder1");
if ( subfolder1== null && space.hasPermission("CreateChildren"))
{
   // create subfolder1  
subfolder1 = space.createFolder("subfolder1");
}
var subfolder2 = space.childByNamePath("subfolder2");
if ( subfolder2== null && space.hasPermission("CreateChildren"))
{
   // create subfolder2  
subfolder2 = space.createFolder("subfolder2");
}

The problem is that this script create the subfolders at the same level of the created folder and not inside it!

Can someone please help me?

Thanks in advance.
7 REPLIES 7

scouil
Star Contributor
Star Contributor
The problem here is that you use "space", which always points to the root of the current space.
What you want to use is the variable "document" that gives you the document just created.

gmce
Confirmed Champ
Confirmed Champ
Thank you very much.
Now it works like a charm

gmce
Confirmed Champ
Confirmed Champ
For the records, if anyone may need to slve the same problem, here is my script:


var database = document.childByNamePath("database");
if ( database== null && space.hasPermission("CreateChildren"))
{
   // create database  
database = document.createFolder("database");
}


var Documentazione = document.childByNamePath("Documentazione");
if ( Documentazione== null && space.hasPermission("CreateChildren"))
{
   // create Documentazione with some subfolders  
Documentazione = document.createFolder("Documentazione");
var Analisi = document.childByNamePath("Documentazione/Analisi");
Analisi = Documentazione.createFolder("Analisi");
var Brochure = document.childByNamePath("Documentazione/Brochure");
Brochure = Documentazione.createFolder("Brochure");
var Manuali = document.childByNamePath("Documentazione/Manuali");
Manuali = Documentazione.createFolder("Manuali");
var Presentazioni = document.childByNamePath("Documentazione/Presentazioni");
Presentazioni = Documentazione.createFolder("Presentazioni");
}


var Installer = document.childByNamePath("Installer");
if ( Installer== null && space.hasPermission("CreateChildren"))
{
   // create Installer  
Installer = document.createFolder("Installer");
}


var Plugin = document.childByNamePath("Plugin e Accessori");
if ( Plugin== null && space.hasPermission("CreateChildren"))
{
   // create Plugin  
Plugin = document.createFolder("Plugin e Accessori");
}


var Sorgenti = document.childByNamePath("Sorgenti");
if ( Sorgenti== null && space.hasPermission("CreateChildren"))
{
   // create Sorgenti  
Sorgenti = document.createFolder("Sorgenti");
}

scouil
Star Contributor
Star Contributor
Hello,

Why do you test
space.hasPermission("CreateChildren")
instead of
space.hasPermission("CreateChildren")
since what you want is create the new folders under "document" not under "space" ?

And I have another question: how are you triggering this rule?
If you trigger it   "oncreate  cm:folder" you don't need any of the checks
Documentazione== null && space.hasPermission("CreateChildren")
(I took "Documentazione" for the example but that's also true for your other folders of course)

The first one will always be true since you just created the folder it can't already have a child named "Documentazione"
The second will always be true too since you just created a folder, I don't see a case where you couldn't add children to a folder you just created.

gmce
Confirmed Champ
Confirmed Champ
Thanks for your suggestions, i started my script from the alfresco backup sample and so i didn't remove the permission check.

I still need the null check because i use share in a local network, some users use the cifs share instead of the web app.
So it is not impossible that someone is using an offline structure stored in his computer and then decides to move it from the pc to my alfresco share folder. That's why i kept the null check.
But you are completely right, check for permissions if i'm able to create the new folder it's totally useless.

I am starting to understand something in alfresco developement but still be a beginner.

scouil
Star Contributor
Star Contributor
Hello,

You'll need to do some tests but I think CIFS also adds the files 1 by 1 even if you add a whole folder structure.
You'll probably get something like:
Create root folder via CIFS -> rule on root folder -> create child folder via CIFS -> rule on child folder -> etc.

So Documentazione will always be null if that's true, whatever your offline structure was.
And you'll have to check that the "Documentazione" from your offline structure merge well with the "Documentazione" created by the rule.
I don't know CIFS but, depending on the OS you're using it might throw a "folder already exists with this name" or rename to "Documentazione(1)". I hope it doesn't.

Good luck on your learning curve. You'll see that Alfresco is really powerful and modular once you get used to it.

gmce
Confirmed Champ
Confirmed Champ
I've made some tests and there is no problem when i copy with cifs an offline folder already structered with subfolders and just an empty folder, everything goes smooth.
The only problem is yet within CIFS if i drag and drop a folder already existant in my alfresco share directory and select "copy here", Windows client will makes a folder named "Folder - copy" and all the subfolders will be renamed "Subfolder - copy" prior the activation of my rule. This way i the null check doesn't find folders matching the rule and create all the subfolders.
That's annoying but i can live with it.

Thanks again for your help.