Category Archives: JavaScript

Javascript: Templating Folders when created by Share Rules


This one has had me perplexed for some time. We seem to get this question just a few times a year but it’s a really good one:

I’m creating a rule on a folder in Share. This rule will create 3 particular subfolders in the new folder that’s being created with a script. How do I accomplish this?

It made sense to me that your script should look like:

var subfolder1 = space.childByNamePath("subfolder1");
subfolder1 = space.createFolder("subfolder1");

As I’ve found out, the “space” counts as the root folder where this new folder is being placed. If this does anything, it will only create the folders in the same directory as your new folder. It took me a while of looking through blogs, articles and our own documentation to find out that this is the way to get it done:

var invoices = document.childByNamePath("Invoices");
invoices = document.createFolder("Invoices");

I tested this with a rule and it works like a champ. It didn’t dawn on me initially to use document to represent the node being created that triggers the rule. And from here we can then do things like copy, change/add aspects, create associations, and so on. 

Happy Scripting!