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?
Solved! Go to Solution.
Views
Replies
Total Likes
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()
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()
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.
Views
Likes
Replies
Views
Likes
Replies