Groovy console help | Community
Skip to main content
Level 2
July 22, 2025
Solved

Groovy console help

  • July 22, 2025
  • 1 reply
  • 320 views

Hi everyone,
I am using the Groovy Console in AEM to update a custom property demo:status to "archieved" for all pages under /content/demo/en/archive that have a cq:template of myproject/components/page/article.
Here is the script: 

def rootPath = "/content/my-site/en/archive"
def resourceResolver = resourceResolverFactory.getServiceResourceResolver(null)
def session = resourceResolver.adaptTo(Session)

def query = "SELECT * FROM [cq:Page] AS s WHERE ISDESCENDANTNODE([${rootPath}]) AND s.[cq:template] = 'myproject/components/page/article'"
def result = session.workspace.queryManager.createQuery(query, "JCR-SQL2").execute()

result.nodes.each { node ->
def contentNode = node.getNode("jcr:content")
contentNode.setProperty("myProject:status", "archived")
println "Updated: ${node.path}"
}

session.save()

 

But I keep getting errors: javax.jcr.PathNotFoundException: jcr:content Can someone help me understand what’s wrong here?

 

Best answer by SantoshSai

Hi @vishal_kagde,

You don’t need to manually get a ResourceResolver - Groovy Console already runs with one.

Try this:

def rootPath = "/content/my-site/en/archive" def templatePath = "/conf/myproject/settings/wcm/templates/article" // Update if full path is needed def resources = getPageManager().getPage(rootPath) .listChildren() .findAll { page -> def content = page.contentResource content != null && content.getValueMap()["cq:template"] == templatePath } resources.each { page -> def content = page.contentResource def modifiableMap = content.adaptTo(com.day.cq.commons.jcr.JcrUtil).getModifiableValueMap(content.resourceResolver, content.path) modifiableMap["myProject:status"] = "archived" println "Updated: ${page.path}" } resourceResolver.commit()

1 reply

SantoshSai
Community Advisor
SantoshSaiCommunity AdvisorAccepted solution
Community Advisor
July 22, 2025

Hi @vishal_kagde,

You don’t need to manually get a ResourceResolver - Groovy Console already runs with one.

Try this:

def rootPath = "/content/my-site/en/archive" def templatePath = "/conf/myproject/settings/wcm/templates/article" // Update if full path is needed def resources = getPageManager().getPage(rootPath) .listChildren() .findAll { page -> def content = page.contentResource content != null && content.getValueMap()["cq:template"] == templatePath } resources.each { page -> def content = page.contentResource def modifiableMap = content.adaptTo(com.day.cq.commons.jcr.JcrUtil).getModifiableValueMap(content.resourceResolver, content.path) modifiableMap["myProject:status"] = "archived" println "Updated: ${page.path}" } resourceResolver.commit()
Santosh Sai
Level 2
July 22, 2025

Thanks,  it worked. Just wanted to share a small improvement I made in the simplified version using [cq:PageContent], I added a null check for node and myProject:status just to be safe in case some nodes were partially created or missing properties.