cancel
Showing results for 
Search instead for 
Did you mean: 

Write a webscript

jdowntain
Champ in-the-making
Champ in-the-making
Hi,

I am new to web scripting and Alfresco. I was wondering if anyone could assist me with creating a webscript that when a user creates a folder from a site they are from that it automatically creates a folder structure like so,

reports (Main Folder)
    #references (sub)
    #data (Sub)
          *raw
          *processed

Thank you in an advanced
6 REPLIES 6

jpotts
World-Class Innovator
World-Class Innovator
I assume you are doing this as a solution to this post.

The solution is not to write a web script, but instead to write server-side JavaScript and trigger it from a rule. A server-side JavaScript file resides in Company Home/Data Dictionary/Scripts.

I think it would also be wise to create a Space Template instead of hardcoding the folders in your script. This has the added advantage of being useful if Share ever supports Space Templates.

So I would do something like this:

- Navigate to Data Dictionary/Space Templates.

- Create a new folder. In that folder create the folder structure you desire. Note the path to the root folder you created. For example, I might create a folder called Reports and in that put my folder structure. The path to that folder is:
/app:company_home/app:dictionary/app:space_templates/cm:Reports

- Now create a file called copy-space-template.js in Data Dictionary/Scripts. That's where your logic will live. The code needs to (1) find your Space Template and then (2) copy the space template to the folder that was just created that triggered the rule. That code might look like:
var results = search.luceneSearch("PATH:\"/app:company_home/app:dictionary/app:space_templates/cm:Reports\"");
var srcFolder = results[0];
srcFolder.copy(space, true);
- Now go configure a rule on the documentLibrary folder in the Share site you want this to be enabled for. Make sure you set the criteria to "instances of a specific type" and the type you select is "folder". You should probably also select "Run in Background". Tell the rule to execute the script you created earlier when a new folder is created that meets this criteria.

What *should* happen is when you create a new folder the rule triggers your server-side JavaScript. The script will find the Reports folder and copy it into the newly-created folder. However, in my testing, my rule goes rogue and ends up creating an infinite number of copies of my Reports folder. I don't know if that's due to a bug or a problem with the approach. Maybe you can try it out and let me know if you have better luck.

If you do try it out, keep an eye on your folder. If it gets bigger and bigger, kill your server before it fills up your database.

If you don't like the Space Template approach, you should be able to follow the JavaScript API documentation to use server-side JavaScript to create new folders instead of copying the folder structure from the Space Template.

Jeff

jdowntain
Champ in-the-making
Champ in-the-making
HI Jeff,

I've attempted your solution but my rules will not run. Here are the steps I've followed and results.

1. Navigate to Data Dictionary / Space Templates
2. Created new folder called "Reports"- PathSmiley Sadworkspace://SpacesStore/09659b2f-731c-427a-bb69-8bb6eb1f75b6)
3. Navigated to Data Dictionary/ Scripts
4. Created content
5. Choose "content"
6. Entered
var results = search.luceneSearch("PATH:\"/app:company_home/app:dictionary/app:space_templates/cm:Reports/"
workspace://SpacesStore/09659b2f-731c-427a-bb69-8bb6eb1f75b6");
var srcFolder = results[0];
srcFolder.copy(space, true);
7. Went to apply rule
8. When- Items are created;
    If All criteria is met;
    Name/ contains/ reports
    Perform action;
    Execute script/Alfresco Lucene/search.
When test is run the folder I create doesn't have any sub folders? Am I doing something wrong?

jpotts
World-Class Innovator
World-Class Innovator
In step 2 you've identified a path, but it is not a path, it is a noderef. Normally, I wouldn't be so picky, but then on step 6 I see you've modified the lucene search by placing the noderef at the end of the path which isn't going to work.

In step 8, for your rule criteria, I don't see any criteria for "cm:folder". You definitely only want the rule to trigger for folders.

Jeff

jdowntain
Champ in-the-making
Champ in-the-making
Jeff,

I saw exactly what I did after reviewing it again. I am still confused as to how to implement a rule when a folder is created within a site folder that it is created along with sub-folders. Are you sure I don't need to create a custom web script for this?

rwalker
Champ in-the-making
Champ in-the-making
You're getting inf loop with the above code because it's copying the space template back to 'space' which is the folder
containing the rule. This was triggering the recursive calls. The code below should work, I opted to grab the space template
by nodeRef for simplicity.

   if (document.isContainer){
      logger.log("CopySpaceTemplate document is Container, attempting to apply space template");
        var spaceTemp = search.findNode("workspace://SpacesStore/d5e86f47-7baa-4ffb-a235-8ddbe2a53e4c");
      spaceTemp.copy(document,true);
      logger.log("CopySpaceTemplate Space moved sucessfully");

   } else {
      logger.log("CopySpaceTemplate document is NOT container");
   }

-Ray

jpotts
World-Class Innovator
World-Class Innovator
Ray,

You are right. Good catch. For some reason I was thinking that if the rule was running against a space, that's what would be in the space variable, but that was a temporary lapse of reason on my part.

Jeff