I am trying to get all the assets inside a smart collection in AEM given the path of the smart collection.
I could do this for a normal collection by getting the node paths under sling:members
But how to get all the assets of a Smart Collection
The data under sling:members is empty because of which my code works only for normal collections but not Smart Collection
I expect to get all the assets under for a smart Collection given the path of the smart collection in java
Here is a simple snippet you can run with AEM Groovy Console:
// https://helpx.adobe.com/experience-manager/6-4/sites/developing/using/reference-materials/javadoc/com/day/cq/dam/api/collection/SmartCollection.html
import com.day.cq.dam.api.collection.SmartCollection;
import com.day.cq.dam.api.Asset;
def SMART_COLLECTION_PATH = "/content/dam/collections/J/Jx4h69ABp_KoLbZJ-8dq/test-collection";
def smartCollectionResource = getResource(SMART_COLLECTION_PATH)
def smartCollection = smartCollectionResource.adaptTo(SmartCollection.class)
smartCollection
.getQuery()
.getResult()
.getNodes()
.each {
def assetResource = getResource(it.path);
def asset = assetResource.adaptTo(Asset.class)
println asset.path
}
The basic gist is that you can get the smart collection resource then adapt it to a SmartCollection from there you can call getQuery
, execute the query, get the nodes and adapt them to Asset objects or just process the nodes directly. In the code above, I print the asset paths.
Even thought the code above is groovy, it is simple enough that you could convert it to java very quickly.