7. JCR Query 2

Since there's a dedicated tutorial covering this topic in detail, we'll focus here only on the specialized elements related to Groovy Scripting.

For more information use the JCR Query 2 Tutorial

Imports

Begin by importing the necessary packages to interact with Magnolia's API within your Groovy script:

import javax.jcr.query.Query
import javax.jcr.query.QueryManager

Provide JCR session

This method call is used to retrieve a JCR session that is linked to a specific workspace, in this case "Website".

def session = MgnlContext.getSystemContext().getJCRSession("website");

Obtaining Query Manager

To execute SQL2 queries, obtain a QueryManager instance from the JCR session:

def queryManager = session.getWorkspace().getQueryManager()

Executing SQL2 Query

Construct and execute SQL2 query using the QueryManager instance:

def sql2 = "SELECT * FROM [nt:base] WHERE ISDESCENDANTNODE('/yourNodePath')"
def query = queryManager.createQuery(sql2, Query.JCR_SQL2)
def result = query.execute()

Processing Query Results

Iterate over the query results to retrieve content nodes:

def nodes = result.getNodes()
nodes.each { node ->
    // Process each node as needed
    println(node.getName())
}