Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

How to get List of file names from folder in crx/de

Avatar

Level 1

Hi 

I want to read all the files present in a folder in crx/de. 

 

So for example I have one folder in crx/de(AEM 6.5) by name DataJson which contains json files.

For example file names are one.json, two.json, three,json.

Now I want to access the name of all the files which is present in DataJson folder in my sling model.

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor
4 Replies

Avatar

Correct answer by
Community Advisor

Avatar

Community Advisor

Hi @SanketJa1 ,

To get a list of file names from a folder in CRX/DE (Adobe Experience Manager), you can use the CRX/DE REST API. Here's how you can do it:

  1. Make a GET request to the following endpoint, replacing folderPath with the path to your desired folder:

    http://localhost:4502/crx/de/query.jsp?path=<folderPath>&1_property=jcr:primaryType&1_property.value=nt:file&p.limit=-1&p.hits=selective&p.nodedepth=1
  2. This request will return an XML response containing information about the files in the folder. You can parse this XML response to extract the file names.

Here's an example of how you can achieve this using Java and the Apache HttpClient library:

 

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

public class CRXDEFileList {
    public static void main(String[] args) {
        String folderPath = "/content/DataJson"; // Replace with your folder path

        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet httpGet = new HttpGet("http://localhost:4502/crx/de/query.jsp?path=" + folderPath + "&1_property=jcr:primaryType&1_property.value=nt:file&p.limit=-1&p.hits=selective&p.nodedepth=1");

        try {
            HttpResponse response = httpClient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            String xmlResponse = EntityUtils.toString(entity);

            // Parse the XML response to extract the file names
            // You can use an XML parser library like JAXB or DOM4J for this

            // Example using DOM4J
            Document document = DocumentHelper.parseText(xmlResponse);
            List<Node> fileNodes = document.selectNodes("//response/properties/property[@name='jcr:content']/property[@name='jcr:data']/property[@name='jcr:lastModified']");
            for (Node fileNode : fileNodes) {
                String fileName = fileNode.valueOf("@name");
                System.out.println(fileName);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

Make sure to include the necessary dependencies for Apache HttpClient and the XML parser library you choose to use.

This code snippet demonstrates how to retrieve the file names from the folder using Java. You can adapt it to your specific use case, such as integrating it into your Sling Model.

Avatar

Community Advisor

@SanketJa1 

 

There are multiple options with examples discussed on https://kiransg.com/2021/11/07/aem-iterating-node-best-practices/

I would suggest Resource Filter Stream


Aanchal Sikka

Avatar

Administrator

@SanketJa1 Did you find the suggestions from users helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Kautuk Sahni