how to get all assets from a folder under content dam folder in aem

sams16001423

21-02-2020

Hi All,

 

I'm new to AEM please let me know if anyone has idea on below scenario.

I'm working on dam assets -  I would like to delete assets from one particular folder after some specific time So Im using scheduler for this requirement,  but wanted to know how to get all assets from a folder So that once i get list of assets from folder will try to read the jcr:data of all assets like modified date and based on that will delete the asset once it completes some time in dam.

 

please suggest if approach is not correct

 

path will be something like this :-  /content/dam/folder

 

Thanks in advance

Answers (2)

Answers (2)

Briankasingli

MVP

22-02-2020

Hello @sams16001423 

You can utilise the JCR API. The JCR API allows you to query nodes in the JCR with the "JCR_SQL2 search queries". I have provided an example for you below where we are querying for all JCR node-type [dam:AssetContent] under the path "/content/dam/my-project". If results are returned, then we will print each jcr:lastModified (the dam:AssetContent node composes of the jcr:lastModified values). Making a query like this is awesome because you don't need to worry about nested folders.

JCR API - JCR_SQL2 Example Search in the Backend:

 

private void doExampleSearch(ResourceResolver resolver) throws RepositoryException {
    Session session = resolver.adaptTo(Session.class);
    QueryManager queryManager = session.getWorkspace().getQueryManager();
    String selectQuery = "SELECT * FROM [dam:AssetContent] WHERE ISDESCENDANTNODE ([/content/dam/my-project])";
    Query query = queryManager.createQuery(selectQuery, Query.JCR_SQL2);
    QueryResult queryResults = query.execute();
    if (queryResults != null) {
        NodeIterator resultNodes = queryResults.getNodes();
        while (resultNodes.hasNext()) {
            Node node = resultNodes.nextNode();
            System.out.println(node.getProperty("jcr:lastModified").getString();
        }
    }
}

 

 Before the implementation code, you should familiarize yourself with JCR_SQL2 queries. You can test your queries directly from http://localhost:4502/crx/de/index.jsp; tools (drop-down menu item) > query. A tutorial for JCR_SQL2 query language for beginners can be found here: https://sourcedcode.com/aem-jcr-sql2-tutorial-and-examples-and-cheatsheet.
I hope this helps.