How do I get all the Assets in a collection in AEM?
I’m trying to get all Assets in an AEM collection given the collection path.
Suppose the path to the collection is
/content/dam/collections/public/-/-XZMzZlhkJKIa7-Mg15h/testing
I need to get all the Assets in this collection.
I checked under content/dam/collections that Assets are not stored in this location
I even tried writing a query in AEM’s query builder that gives type:dam:asset
and Path: Location of the collection
I can’t get any results.
I just need to get all the Assets under the collection in Java or QueryBuilder
Solution
Storing Assets in a collection is not a good idea because Assets can be part of multiple collections. Just like in a playlist, Assets are referenced only in collections.
Each collection has a child node “sling:members”. This child node has a (multivalued) property, sling:resources, which holds a reference to each Assets of that collection member.
Most importantly, sling:members also have child nodes of type nt:unstructured, and each Assets has the properties sling:resource and assets path.
Therefore, you can iterate over property values or child nodes to get references to Assets and then access them in their original locations.
HTH