cancel
Showing results for 
Search instead for 
Did you mean: 

Problem With Javascript to Dynamically Create Spaces/Folders

th1s_pr0gramm3r
Champ in-the-making
Champ in-the-making
Hi everyone, I am currently working with Alfresco 4.0.d community edition on a Oracle Linux 6 virtual machine, using Firefox as my browser.

Most recently, I had been attempting to create a script which could be executed via a rule that would dynamically create sub-folders as new items entered a space/folder.

Below is the script I created…


function main()
{
   var rootSpaceName = companyhome.childByNamePath("mainFolder");
   var childList = rootSpaceName.children;
   var count = childList.length;
   
   for(var i = 0; i <= count; i++)
   {
      var childTitle = childList[i].properties.title;
      var childAuthor = childList[i].properties.author;
      var child = childList[i];
      
      if(child.isContainer == false)
      {
         for(var j = 0; j < count; j++)
         {
            var newChildName = childList[j].properties.name;
            var newChild = childList[j];
            
            if((newChild.isContainer == true) && (childTitle.equals(newChildName)))
            {
               var newSpaceName = rootSpaceName.childByNamePath(newChildName);
               var newChildList = newSpaceName.children;
               var newCount = newChildList.length;
               
               for(var k = 0; k < newCount; k++)
               {
                  var newNewChildName = newChildList[k].properties.name;
                  var newNewChildAuthor = newChildList[k].properties.author;
                  var newNewChild = newChildList[k];
                  
                  if((newNewChild.isContainer == true) && (newNewChildAuthor.equals(childAuthor)))
                  {
                     var currentSpace = newSpaceName.childByNamePath(newNewChildName);
                     child.move(currentSpace);
                  }
               }
            }
            else
            {
               var newSpaceName = rootSpaceName.createFolder(childTitle);
               var newNewSpaceName = newSpaceName.createFolder(childAuthor);
               child.move(newNewSpaceName);
            }
         }
      }
   }
   return;
}

main();

The logic seems fine (at least in my head, but when I try to implement the script as admin, i.e. put it into the scripts folder within the data dictionary and try to create a rule to execute the script from within my "mainFolder", nothing happens. I tried using the Alfresco javascript debugger, and what I found was that the original
var rootSpaceName = companyhome.childByNamePath("mainFolder");
doesn't seem to find the correct folder, instead the value that it returns in the debugger is: A valid SecureContext was not provided in the RequestContext.

Then stepping into the script, I find that in the else condition, where it declares
var newSpaceName = rootSpaceName.createFolder(childTitle);
, I get a pop-up exception stating that
org.mozilla.javascript.WrappedExectution: Wrapped java.lang.IllegalArgumentException: Node Name is a mandatory parameter (workspace://SpacesStore/792dd872-5cfe-4a65-a509-4b9979f803d4#41)
. Which I assume is because the original root node cannot be found to begin with?

Does anyone see what the problems might be with this script, as well as what the aforementioned errors could be caused by? Additionally, I was trying to write out to the console in the Alfresco javascript debugger using
console.log("Write this out");
, but it seemed to keep resulting in an error stating the console was not specified. Does anyone know how to actually write out to the debugger console?
1 REPLY 1

jpotts
World-Class Innovator
World-Class Innovator
The problem is here:
var newSpaceName = rootSpaceName.createFolder(childTitle);

Because the code as written will try to create a folder with the same name as the document that is already in the mainFolder. For example, if I have a document called "test.txt" your code tries to create a folder called "test.txt" and that's not going to fly–nodes must be uniquely named in a given folder.

To answer your logging question you can use logger.log but you have to turn on debugging in log4j.properties by setting:
log4j.logger.org.alfresco.repo.jscript.ScriptLogger=debug

Another tool you might be interested in is Florian Maul's JavaScript Console. It lets you run JavaScript interactively in the Share UI. Use "print" to write statements to that console.

Jeff