Some­times when I need to parse a large CQ web­site using Java, I want to list the chil­dren of a page based on the tem­plate. This can be very help­ful when cre­at­ing a carousel dis­play­ing fea­tured pages, or work­ing on a search func­tion­nal­ity, as in both cases we need to cre­ate a list of pages we want to dis­play back to the user.

There are a few ways to do this: I can use the query builder to cre­ate a query based on the cq:template prop­erty, but the code can be a bit com­plex. I could also sim­ply iter­ate over the chil­dren of the page and read the prop­erty to cre­ate my list, but it’s a rather inel­e­gant approach.

So I had a look through the CQ Page API, and found a nice and easy-to-use func­tion for this: listChildren(filter). It fits my require­ment per­fectly, as I will illus­trate here.

Using this func­tion you can lever­age an out of the box fil­ter, the Page­Filter, as such:

Iter­a­tor< Page > it = homePage.listChildren(new PageFilter());

Or cre­ate a cus­tom one based on your own require­ments. For instance in our case if we want to return the pages using the tem­plate named “pro­duct­page”, we sim­ply have to define the fol­low­ing filter:

class Pro­duct­Page­Filter imple­ments Filter {

pub­lic boolean includes(Object ele­ment) {
Page p = (Page) ele­ment;
return (p.getProperties().get(NameConstants.NN_TEMPLATE, “”)