Groovy Script In AEM
CMS

Groovy Script in AEM

Whether you have the need to manipulate AEM tree structure or the content in it at a bulk/small level or you are required to generate custom reports now and then of the AEM content then you are at the right place. Groovy can do it all with very little coding and that too, without any change in the core code of your AEM project.

Introduction:

Groovy is a dynamic object-oriented programming language which is based on Java platform itself.

The developers of groovy designed it in such a way that learning/understanding groovy comes naturally to a java developer so the learning curve to learn groovy for a java developer is very minimal.

With groovy, one can manipulate content in the JCR, call OSGi services, or execute arbitrary code using the AEM, Sling, or JCR APIs

To use and utilize the power and ease of groovy in AEM, we need to install Groovy Console package in AEM

Installation:

To use groovy scripting in AEM, we just need to install a groovy console package in AEM.

1. Download the groovy console package from https://github.com/OlsonDigital/aem-groovy-console

check version compatibility for specific AEM version

2. Install the downloaded package using crx package manager

3. To verify the installation, open http://<domain>:<port>/etc/groovyconsole.html in browser to view the groovy console

Different parts of Groovy Console:

 

Figure 1: Screenshot of Groovy Console

In the screenshot of groovy console above few important sections are marked with numbers. We will discuss each section below

1. Themes: once can set his preferred theme to style the groovy console and coding area in it.

2. Run Script button: this button executes the currently written code in the coding area (section 6)

3. New button: this button will clear out the coding area for you to start with new groovy script.

Be careful to save your current script before pressing this button.

4. Open button: this will open a box showing all the available saved scripts to load in the current coding area to use. This will also show the sample scripts provided by the groovy console under samples folder, which you may refer to know how to perform few basic operations in AEM using groovy.

Be careful to save your current script before pressing this button.

5. Save button: this will show a popup box to fill in a filename to save your current script with that filename. If you are editing an existing script, then it will still open this box with existing script’s filename in it. If you wish to save it in different file, then change the name and save or else leave the name as is and save.

6. Service or adapter name search box: this is very helpful section where you may start typing the name of any OSGI service or adapter name to look up and this will show you that service or adapter if available. If you choose that service or adapter, then a piece of script/statement would be inserted in the end of your current script which you may cut and place anywhere else as well you get access to the instance of that service or adapter.

7. Coding Area: this section just under Data section is the area where you will write your script.

There are few more sections in the groovy console. Some of the most important of them all are Bindings, Imports and Methods. These sections tell you about all the implicit binding objects, imports and methods available for you to directly use inside your groovy script.

Examples:

To help you further on how you can achieve certain basic real-life objectives in AEM using groovy we will see few examples now

1. To find the pages under a specific path which are activated after a specific date

The objective is to print the pages with their full AEM JCR paths in the groovy console itself which are activated after a specific date and this search should be carried out for pages under a specific path only.

 

2. To add a property to all the pages under a specific path

The objective here is to add a property with some value to all the pages which are under a specific path.

 

3. To remove a property from all pages under a specific path

The objective is to remove/delete a specific property from all pages which are under a specific path

 

4. To generate a report as an excel of active pages along with their properties

The objective is to generate a report of all the active pages which are under a specific path and this report should also include the properties and their values of those pages.

Here we would be find all the active pages, very similar to example 1 and then we would be using HSSFWorkbook API to create a workbook/spreadsheet for those pages with their properties and values resp.

The created workbook is still not a real file for us to access after script finishes. To achieve that we would create an asset of .xlsx file with that workbook data in AEM dam.

 

About The Author