Studio

Javascript Expression Examples

Updated: August 19, 2019 Page Information Edit on GitHub

Javascript expressions are useful in various use cases:

  1. To create filters that can be used either from Nuxeo Studio Designer or directly in your code using the nuxeo-filter element, in the expression property.
    They can be used to determine the suitable conditions to display an action, a menu from the left menu bar, any pages, and more globally, any element.

  2. To implement specific business logic, you can use Javascript expression in your custom automation chains and also in the Polymer section of your element.

  3. To map parameters from a page provider NXQL query into the corresponding variables. In this context, Javascript variables need to be provided as a Javascript object, e.g. {"key1": "value1", "key2": "value2"}. For a detailed example using them, you may refer to How to Display a Children Documents Listing.

Here is a list of the main Javascript expressions:

Criteria Javascript Expression Corresponding Content View Query Parameter
Document title document.title #{currentDocument.dc.title}
Document id document.uuid #{currentDocument.id}
Document type document.type #{currentDocument.type}
Document name document.path.substr(document.path.lastIndexOf('/') + 1) #{currentDocument.name}
Document version document.properties['uid:major_version']
document.properties['uid:minor_version']
#{currentDocument.uid.major_version}
#{currentDocument.uid.minor_version}
Document property document.properties["<prefix>:<property_name>"] #{currentDocument.< prefix >.< property_name >}
Document is a version (not a live document) document.isVersion() #{currentDocument.isVersion}
Document has facet document.facets.indexOf('facet') #{currentDocument.hasFacet('facet')}
User ID user.id #{currentUser.name}
User email user.email #{currentUser.email}
User has permission on document user.hasPermission(document, '<permission>') #{nxd:hasPermission(document, '< permission >')}
Group membership user.properties.groups.indexOf('< groupname >') #{currentUser.isMemberOf('< groupname >')

You can get more attributes from the Expression Editor available in Nuxeo Studio Modeler:

Javascript Expression Document
Javascript Expression Document

Javascript Expression User
Javascript Expression User

More options
You can also use Special NXQL Properties to build your expressions, and the Functions object.

From this list, you can compose some conditions:

Condition Javascript Expression
Document is a picture and the user has right permission doc.type !== 'Picture' && user.hasPermission(doc, 'Write')
Document doesn't have the "Retention" facet or its major version is under 1.0 document.facets.indexOf("Retention") < 0 || doc.properties['uid:major_version'] <= 1
User doesn't belong to the external group user.properties.groups.indexOf('external') < 0
a month ago manonlumeau Final review
History: Created by manonlumeau

We'd love to hear your thoughts!

All fields required