Alfresco 3: Writing and Executing Scripts

4 min read


Alfresco 3 Cookbook

Alfresco 3 Cookbook

Over 70 recipes for implementing the most important functionalities of Alfresco

The reader can benefit from the previous article on Implementing Alfresco JavaScript API Functionalities.


Alfresco, like any other enterprise open source framework, exposes a number of APIs including Alfresco SDK (Software Development Kit) a set of development tools that allows the creation of an application for a certain software package or framework and JavaScript API.

Available JavaScript APIs

Alfresco JavaScript API exposes all important repository objects as JavaScript objects that can be used in a script file. The API follows the object-oriented programming model for well known Alfresco concepts such as Nodes, Properties, Associations, and Aspects.

The JavaScript API is capable of performing several essential functions for the script developer, such as:

  • Create Node, Update Node: You can create, upload, or update files using these.
  • Check In/Check Out: You can programmatically check-out and check-in your content.
  • Access Rights Management Permissioning: You can manage your content’s security aspects.
  • Transformation: You can transform your content using this. For example, you want to generate a PDF version of your MS-Office document.
  • Tagging: Tagging APIs will help you tag your contents.
  • Classifying: You can categorize or classify your contents using this.
  • People: Using these APIs, you can handle all user-and group-related operations in your script; such as creating a new user, changing the password of a user, and so on.
  • Searching: One of most important and powerful APIs exposed. You can search your contents using these APIs. You can perform Lucene-based search or XPath-based search operations using these APIs.
  • Workflow: You can manage the tasks and workflows in your system using these APIs and services.
  • Thumbnail: Exposes APIs to manage the thumbnail operations of various content items.
  • Node operations: You use these APIs to perform several node-related functions such as Manage Properties, Manage Aspects, copying, deleting, moving, and so on.

Thus, as you can see, pretty much most of the things can be done in a JavaScript file using these APIs.

However, one thing is important, that you should not mix the usual JavaScript code you write for your HTML or JSP web pages. Those scripts are executed by your browser (this means, at the client side). The scripts you write using Alfresco JavaScript API are not client-side JavaScript file – this means these do not get executed by your browser. Instead, they get executed in your server and the browser has nothing to do in these scripts.
It is called JavaScript API since the APIs are exposed using the ECMA script model and syntaxes. The programs you develop using these APIs are written in JavaScript language.

The JavaScript API model

Alfresco has provided a number of objects in the JavaScript API – these are more usually named as Root Scope Objects. These objects are your entry point into the repository. Each of the root level objects refers to a particular entity or functional point in the repository. For example, userhome object refers to the home space node of the current user. Each of these objects presents a number of properties and functionalities, thus enabling the script writer to implement several different requirements. For example, the statement will return the name of the root folder of the current user.

Some important and most frequently used root scope objects are:

  • Companyhome: Returns the company home script node object
  • Userhome: Returns the home folder node of the current user
  • Person: Represents the current user person object
  • Space: Stands for the current space object
  • Document: Returns the currently selected document
  • Search: Offers fully functional search APIs
  • People: Encapsulates all functionalities related to user, groups, roles, permissions, and so on.
  • Sites: Exposes the site service functionalities
  • Actions: Provides invocation methods for registered actions
  • Workflow: Handles all functionalities related to workflow implementation within the repository

Among these, companyhome, userhome, person, space, and document objects represent Alfresco Node objects and allow access to the properties and aspects of the corresponding node object. Each of the node objects provides a number of APIs which are termed ScriptNode API.

The others – search, people, sites, workflow, and actions – expose several methods that would help you implement specific business requirements. For example, if you want to write a script that searches some documents and contents, you would use the search API. If you want to create a new user – the people API will help you.



Please enter your comment!
Please enter your name here