Coding with the Hub #5 - Creating an Alfresco Script Root Object

Showing results for 
Search instead for 
Did you mean: 

Coding with the Hub #5 - Creating an Alfresco Script Root Object

Alfresco Employee
1 0 1,085

Alfresco provides a Repository JavaScript framework that can be used to develop custom logic associated to features like Folder Rules.

This framework can be extended by adding new Root Objects, so they can be used as a regular object in the JavaScript scripts. Alfresco Documentation provides the instructions to implement this approach.

An additional project, available in, has been created to provide a sample use case of this extension mechanism.

Alfresco Repository Spring beans, defined in XML files packaged in JAR libraries, can be used in JavaScript server side scripts accesing to Spring Web Context. The following sample code gets a reference of sysAdminParams Spring bean from a JavaScript environment.

var context =;
var sysAdminParams = context.getBean('sysAdminParams',;

Since this approach is valid for some use cases, it doesn't work when using scripts deployed to "Repository > Data Dictionary > Scripts" folder due to security restrictions. Extending the JavaScript server side API to add the sysAdmin root object enables this kind of operations to be executed in this kind of scripts.

Without the addon, the code to add a value from sysAdmin to a document using a folder rule could be similar to this one.

var context =;
var sysAdminParams =
context.getBean('sysAdminParams',;["cm:description"] = sysAdminParams.getAlfrescoHost();;

This will produce an error in Alfresco log when executing the code associated to the folder rule, as it's considered to be executed on an insecure environment:

Caused by: org.mozilla.javascript.EcmaError: ReferenceError: "Packages" is not defined.

In order to avoid this behaviour, with the alfresco-script-root-object project deployed, the code can be expressed as:["cm:description"] = sysAdmin.getAlfrescoHost();;

And this JavaScript code will be executed successfully, since the code is provided from the Repository itself.

Video Recording

Additional details are provided in the following video.


About the Author
Angel Borroy is Hyland Developer Evangelist. Over the last 15 years, he has been working as a software architect on Java, BPM, document management and electronic signatures. He has been working with Alfresco during the last years to customize several implementations in large organizations and to provide add-ons to the Community based on Record Management and Electronic Signature. He writes (sometimes) on his personal blog He is (proud) member of the Order of the Bee.