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

cancel
Showing results for 
Search instead for 
Did you mean: 

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

angelborroy
Alfresco Employee
0 0 162

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 https://github.com/aborroy/alfresco-script-root-object, 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 = Packages.org.springframework.web.context.ContextLoader.getCurrentWebApplicationContext();
var sysAdminParams = context.getBean('sysAdminParams', Packages.org.alfresco.repo.admin.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 = Packages.org.springframework.web.context.ContextLoader.getCurrentWebApplicationContext();
var sysAdminParams =
context.getBean('sysAdminParams', Packages.org.alfresco.repo.admin.SysAdminParams);
document.properties["cm:description"] = sysAdminParams.getAlfrescoHost();
document.save();

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:

document.properties["cm:description"] = sysAdmin.getAlfrescoHost();
document.save();

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 http://angelborroy.wordpress.com. He is (proud) member of the Order of the Bee.