Do we have any way to find empty page list in aem content by using query builder or groovy script | Community
Skip to main content
Level 3
January 2, 2024
Solved

Do we have any way to find empty page list in aem content by using query builder or groovy script

  • January 2, 2024
  • 3 replies
  • 1664 views

Hi,

 

Do we have any way to find empty page list in aem content by using query builder or groovy script.How can we find list of pages if it has only pagetitle component ?. I have tried with below link  it's giving list of pages which has page title and other components as well but the list is very big since we have page title component in many of pages. I need the page list which has only pagetitle component and it should not any other componnet in page.

https://gist.github.com/ahmed-musallam/780d92ee1ea30c7101054506364d6a25

 

 

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 Raja_Reddy

Hi @vijitha 
you can use a Groovy script with the AEM QueryBuilder API. Below is an example script that performs a similar operation:

def queryBuilder = sling.getService(QueryBuilder)

def query = queryBuilder.createQuery(PredicateGroup.create([
// Add conditions to match your template
queryBuilder.path("/content/your-site"),
queryBuilder.template("/apps/your-site/templates/your-template"),
// Exclude pages with other components
queryBuilder.and(
queryBuilder.exists("jcr:content/your-component", "true"),
queryBuilder.not().exists("jcr:content/*")
)
]), sling.getRequest())

def searchResults = query.getResult().getHits()

searchResults.each { hit ->
def pagePath = hit.getPath()
println("Page Path: $pagePath")
}
________________________________________
Using Query Builder
ex: type=cq:Page
path=/content/your-site
property=jcr:content/cq:template
property.value=/apps/your-site/templates/your-template
property.operation=like
property.1_property=jcr:content/your-component
property.1_property.operation=exists
property.2_property=jcr:content
property.2_property.operation=not
property.3_property=jcr:content/*
property.3_property.operation=exists

Thanks.


3 replies

Raja_Reddy
Community Advisor
Raja_ReddyCommunity AdvisorAccepted solution
Community Advisor
January 2, 2024

Hi @vijitha 
you can use a Groovy script with the AEM QueryBuilder API. Below is an example script that performs a similar operation:

def queryBuilder = sling.getService(QueryBuilder)

def query = queryBuilder.createQuery(PredicateGroup.create([
// Add conditions to match your template
queryBuilder.path("/content/your-site"),
queryBuilder.template("/apps/your-site/templates/your-template"),
// Exclude pages with other components
queryBuilder.and(
queryBuilder.exists("jcr:content/your-component", "true"),
queryBuilder.not().exists("jcr:content/*")
)
]), sling.getRequest())

def searchResults = query.getResult().getHits()

searchResults.each { hit ->
def pagePath = hit.getPath()
println("Page Path: $pagePath")
}
________________________________________
Using Query Builder
ex: type=cq:Page
path=/content/your-site
property=jcr:content/cq:template
property.value=/apps/your-site/templates/your-template
property.operation=like
property.1_property=jcr:content/your-component
property.1_property.operation=exists
property.2_property=jcr:content
property.2_property.operation=not
property.3_property=jcr:content/*
property.3_property.operation=exists

Thanks.


vijithaAuthor
Level 3
January 17, 2024

Hi @raja_reddy 

Thanks for your reply. I have tried with above query in my local it's giving the list of pages which has pagetitle componenet and also other component. I need the page list where the page is having only page title component.

Can you suggest how to get the empty page list in aem ?

Raja_Reddy
Community Advisor
Community Advisor
January 18, 2024

Hi @vijitha 
check this query
type=nt:unstructured
path=/content/countries
property=sling:resourceType
property.value=xxx/components/xxx/xxx
property.operation=like
p.limit=-1

Madhur-Madan
Community Advisor
Community Advisor
January 3, 2024

Hi @vijitha,
In Adobe Experience Manager (AEM), you can use Query Builder to find pages with specific criteria, such as those containing only the pagetitle component. However, directly querying for pages based on the existence of a single component might not be feasible, as Query Builder primarily searches for properties and nodes, not individual components.

You can try it with groovy scripts

import com.day.cq.wcm.api.Page import com.day.cq.wcm.api.PageManager // Obtain the PageManager PageManager pageManager = resourceResolver.adaptTo(PageManager.class) // Function to check if a page contains only the pagetitle component def hasOnlyPageTitleComponent(Page page) { def jcrContent = page.getContentResource("jcr:content") if (jcrContent != null) { def components = jcrContent.getChildren().collect { it.getName() } return components.size() == 1 && components.contains("pagetitle") } return false } // Traverse the content structure to find pages with only pagetitle component def findPagesWithOnlyPageTitleComponent(Page page) { if (hasOnlyPageTitleComponent(page)) { log.info("Page Path: " + page.getPath()) } else { page.listChildren().each { childPage -> findPagesWithOnlyPageTitleComponent(childPage) } } } // Start traversal from the root path def rootPath = "/content" def rootPage = pageManager.getPage(rootPath) if (rootPage != null) { findPagesWithOnlyPageTitleComponent(rootPage) } else { log.error("Root page not found") }
vijithaAuthor
Level 3
January 18, 2024

Hi @madhur-madan 

 

Thanks for your suggestion. I am new to Groovy script, Is above script will give the page list where we have only page title component or it the list of page where we have pagetitle and other componets as well and when I tried to execute above script it's giving only execuation time and I don't see ant println in it. Please correct me if I am not correct.

Basically I need empty pagelist for over all site or if page has only title component and it should not contain any other component.

kautuk_sahni
Community Manager
Community Manager
January 8, 2024

@vijitha Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.

Kautuk Sahni