Groovy Console Integration and executing scripts in AEM

Steps to install Groovy in AEM.

Below Github have the steps to install the latest Groovy console
https://github.com/icfnext/aem-groovy-console

You can also navigate to the releases section and download the required package.
https://github.com/icfnext/aem-groovy-console/releases

I have used 12.0.0 version for this demo.

  1. Download the Groovy Package (Direct link to the package )
  2. Upload the package to AEM via Package manager http://localhost:4502/crx/packmgr/index.jsp
  3. Once the package is installed, you should be able to access the groovy console http://localhost:4502/etc/groovyconsole.html



Note: From 13.0.0 release, the paths were changed from /etc/ to /apps/ to access Groovy Console

Sample Groovy Scripts:
Printing a simple String
println 'Hello World';

Click on Run Script
Displays Hello World in the Console



Get a Page Object

final def page = getPage('/content/aem-flash')
println 'page path is: ' + page.path;

Result: page path is: /content/aem-flash

Querying and displaying page names

def buildQuery(page) {
    def queryManager = session.workspace.queryManager;
    def statement = 'SELECT * FROM [cq:Page] AS s WHERE ISDESCENDANTNODE(s,\'/content/aem-flash\')';
    queryManager.createQuery(statement, 'sql');
}

final def page = getPage('/content/aem-flash')
final def query = buildQuery(page);
final def result = query.execute()

result.nodes.each { node ->
    println 'node path is: ' + node.path
}

Result:
node path is: /content/aem-flash/en
node path is: /content/aem-flash/fr

Update the data on the nodes

result.nodes.each { node ->
    println 'node path is: ' + node.path
    node.set('customTitle', 'Title Set from Groovy');
    session.save();
}


Comments

Post a Comment

Popular posts from this blog

Delete DAM Asset using Workflow

AEM Project Maven Build - Easy setup and steps to run commands via command line