AEM Groovy script to extract XFs with property on it | Community
Skip to main content
rsl_lucky
Level 4
May 29, 2024
Solved

AEM Groovy script to extract XFs with property on it

  • May 29, 2024
  • 1 reply
  • 1001 views

Hi All,

We have requirement were need to extract all Experience fragments details & its corresponding ID attribute present on it under a particular folder. Each and every Experience fragment consists of an unique ID attribute on them. Try with Query debugger but it fetches all XFs under a folder but not its ID, we require ID and its corresponding XF path details, need of groovy script to extract the details. Would really appreciate if anyone share there inputs on the same. 

__PRESENT

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by sravs

Hi @rsl_lucky ,

try below snippet, update PROJECT_FOLDER with the folder name under which you need to get the fragments details. Here I'm getting jcr:uuid, if you have any specific property you can update the script accrodingly.

 

 

import javax.jcr.query.Query; import javax.jcr.Node; import java.util.List import groovy.transform.Field def query = createSQL2Query() def result = query.execute(); def rows = result.rows def total = 0 rows.each{ raw -> Resource res = resourceResolver.getResource(raw.path) Page page = pageManager.getContainingPage(res.path) println raw.path println page.getProperties().get("jcr:uuid", String.class) total++ } println "Total :: "+ total def createSQL2Query(){ def queryManager = session.workspace.queryManager def statement = "select page.[jcr:path] from [cq:Page] AS page INNER JOIN [cq:PageContent] AS jcrContent ON ISCHILDNODE(jcrContent, page) where ISDESCENDANTNODE(page, '/content/experience-fragments/{PROJECT_FOLDER}') and jcrContent.[jcr:uuid] IS NOT NULL"; //println statement def query = queryManager.createQuery(statement,Query.JCR_SQL2) query.setLimit(3000); query.setOffset(0); query }

 

 

 

 

1 reply

sravs
Community Advisor
sravsCommunity AdvisorAccepted solution
Community Advisor
May 29, 2024

Hi @rsl_lucky ,

try below snippet, update PROJECT_FOLDER with the folder name under which you need to get the fragments details. Here I'm getting jcr:uuid, if you have any specific property you can update the script accrodingly.

 

 

import javax.jcr.query.Query; import javax.jcr.Node; import java.util.List import groovy.transform.Field def query = createSQL2Query() def result = query.execute(); def rows = result.rows def total = 0 rows.each{ raw -> Resource res = resourceResolver.getResource(raw.path) Page page = pageManager.getContainingPage(res.path) println raw.path println page.getProperties().get("jcr:uuid", String.class) total++ } println "Total :: "+ total def createSQL2Query(){ def queryManager = session.workspace.queryManager def statement = "select page.[jcr:path] from [cq:Page] AS page INNER JOIN [cq:PageContent] AS jcrContent ON ISCHILDNODE(jcrContent, page) where ISDESCENDANTNODE(page, '/content/experience-fragments/{PROJECT_FOLDER}') and jcrContent.[jcr:uuid] IS NOT NULL"; //println statement def query = queryManager.createQuery(statement,Query.JCR_SQL2) query.setLimit(3000); query.setOffset(0); query }

 

 

 

 

rsl_lucky
rsl_luckyAuthor
Level 4
May 29, 2024

Thanks @sravs  for quick turn around on it. When ran the script can find few issues like in below screenshot.

Try to resolve the resource by addition of imports but it didn't resolve. Can you share inputs on it ?

rsl_lucky
rsl_luckyAuthor
Level 4
May 29, 2024

Did some tweaks on it and can see the required results now. Thank you.