Unlocking the power of JavaScript in Alfresco

Alfresco is a powerful open source content management platform. One thing that makes Alfresco great is the span of tools you can leverage to extend your Alfresco deployment. For developers, this means finding familiar ground without looking very far. Alfresco’s server side JavaScript processor is one of those familiar friends to developers, analysts, and savvy business users alike. To some, JavaScript is a quick method for getting a task done in a rule or through direct execution in the JavaScript Console. For others the JavaScript api is an easy way to build custom REST interfaces that allow for quick integration with other systems.

Server-side JavaScript

To be clear, this post is speaking pointedly to JavaScript that is executed on the server side. Not to be confused with JS being run in your client (browser). The distinction is important to understand. Alfresco leverages Rhino, an “open-source implementation of JavaScript written entirely in Java”. Rhino takes JavaScript executed by users and compiles it into Java that can be run in the JVM. This takes the work out of translating or transforming syntax and gives Java applications an easy point of access to developers who are more accustomed to client side development.

Running Server-side JavaScript

There are several ways to incorporate Alfresco’s JavaScript API into your project. The three primary points of access are through:

WebScripts

WebScript controllers are often implemented using server-side JavaScript. You can find several examples in Alfresco’s source or community projects.

Rules

Rules can be configured to execute a script. The list of populated scripts is pulled from the Data Dictionary/Scripts directory in Company Home. This is an easy way to perform quick actions on spaces within Alfresco. Add metadata or tag incoming content for easier search.

JavaScript Console

This Alfresco Share add-on is a great way to run administrative tasks quickly without any downtime. The add-on must first be installed before you can access the JavaScript Console in the Admin Tools menu.

The great thing about using Rhino is that you have access to all of the classes running in the JVM you’re compiling to. This means you can take advantage of the full library of java tools and services from JavaScript. By using the full class reference in your declaration you can easily extend your JS toolset.

Here is a quick example that was run in the JavaScript Console.

Script:
logger.log("Starting script.");
var file = org.alfresco.util.TempFileProvider.createTempFile("temp-","js-test");
logger.log(file.toString());
if(file.delete()){
logger.log("Temp file deleted.");
}

logger.log("Done.");

 

Output
DEBUG - Starting script.
DEBUG - /var/folders/9z/yjfr259561j970x03db0b02r0000gn/T/Alfresco/temp-1287146040327544388js-test
DEBUG - Temp file deleted.
DEBUG - Done.

You can see how this can be very useful for accessing public methods to do quick functions. For most service calls you should continue to use Alfresco’s native JavaScript API to take full advantage of instantiated services. For more information on how Alfresco uses Rhino, take a look at the source.

Leave a Reply

Your email address will not be published. Required fields are marked *

From our Blog...

Configuring Alfresco SAML SSO Module with Okta IdP

Alfresco recently released a new patch for their SAML Single Sign On solution module. This module allows Alfresco user’s to configure their Alfresco installation with their Single Sign On (SSO) Identity Provider. In this tutorial, I’ll explain the process of configuring Okta to be used with the module. Note: This tutorial is assuming you’ve followed… Read more »

Read More

Content Migration: Being Prepared

Much like negotiating a treaty between two countries who do not share a common language, someone will be faced with the task of translating. If that translator is not properly prepared the outcome might create more problems than it solves.

Read More

Debugging and Integration Testing in Alfresco SDK 3.0

Alfresco has updated its SDK! See our articles here and here about the basics. In its current state, SDK 3.0 doesn’t support unit testing. It does, however, have a robust Integration Testing framework which, in many ways, covers the same ground and then some. In this article I’ll be going into the basics of Integration… Read more »

Read More