Radically easy to access on brand approved content for distribution and omnichannel performant delivery. AEM Assets Content Hub and Dynamic Media with OpenAPI capabilities is now GA.
It is very common scenario where we need to get all the child nodes under a parent node in AEM.
We can use below code :
List childrenList = new ArrayList<>();
public void getAllChildNodes(String parentPath) {
Resource resource = resolver.getResource(parentPath);
collectChildList(resource);
}
private void collectChildList(Resource resource) {
childrenList.add(resource);
if (resource.hasChildren()) {
Iterable ni = resource.getChildren();
for (Resource res : ni) {
collectChildList(res);
}
}
}
ChildrenList will have all the resources under your parent node.
Hope this helps!!