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.
Solved! Go to Solution.
Views
Replies
Total Likes
Hi @SanketJa1
You can use query builder or resource iterations to get the file name.
https://stackoverflow.com/questions/42013831/sling-best-way-to-get-children-resources-ordered
QueryBuilder : https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/get-child-pages-using-node...
Sling API : https://sling.apache.org/apidocs/sling10/org/apache/sling/api/resource/Resource.html#getChildren--
Hi @SanketJa1
You can use query builder or resource iterations to get the file name.
https://stackoverflow.com/questions/42013831/sling-best-way-to-get-children-resources-ordered
QueryBuilder : https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/get-child-pages-using-node...
Sling API : https://sling.apache.org/apidocs/sling10/org/apache/sling/api/resource/Resource.html#getChildren--
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:
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
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.
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
@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.
Views
Replies
Total Likes
Views
Likes
Replies