if ACS Commons Report Builder is not working you or maybe your AEM environment does not have ACS Commons installed (as it should), you can create a custom servlet for this work.
To create a servlet for exporting custom page properties as a CSV file, you can follow the example below. This servlet will be accessible at /bin/exportcsv with a parameter rootPage indicating the root page from which to start the export.
First, create a Java class for the servlet, for example, ExportCsvServlet.java:
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.api.servlets.ServletResolverConstants;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.io.IOException;
@Component(service = Servlet.class, property = {
ServletResolverConstants.SLING_SERVLET_PATHS + "=/bin/exportcsv",
ServletResolverConstants.SLING_SERVLET_METHODS + "=" + HttpConstants.METHOD_GET
})
public class ExportCsvServlet extends SlingSafeMethodsServlet {
@3214626
private ResourceResolverService resourceResolverService;
@9944223
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
// Get the root page path from the request parameter
String rootPagePath = request.getParameter("rootPage");
// Set response headers for CSV
response.setContentType("text/csv");
response.setHeader("Content-Disposition", "attachment; filename=exported_properties.csv");
// Get the CSV content for the specified root page
String csvContent = getCSVContent(rootPagePath);
// Write CSV content to the response
response.getWriter().write(csvContent);
}
private String getCSVContent(String rootPagePath) {
// Retrieve the root page resource
Resource rootPageResource = resourceResolverService.getResource(rootPagePath);
// Implement logic to recursively traverse child pages and fetch pageTitle property
StringBuilder csvContent = new StringBuilder();
csvContent.append("pageTitle\n");
if (rootPageResource != null) {
collectPageTitles(rootPageResource, csvContent);
}
return csvContent.toString();
}
private void collectPageTitles(Resource pageResource, StringBuilder csvContent) {
// Fetch pageTitle property
String pageTitle = pageResource.getValueMap().get("pageTitle", String.class);
// Append pageTitle to CSV content
if (pageTitle != null) {
csvContent.append(pageTitle).append("\n");
}
// Recursively process child pages
for (Resource child : pageResource.getChildren()) {
collectPageTitles(child, csvContent);
}
}
}
In this example:
- The servlet is annotated with the necessary information for it to be registered and accessible at the specified path (
/bin/exportcsv).
- The
doGet method is called when a GET request is made to the servlet. It retrieves the rootPage parameter, sets up the response headers for CSV, and writes the CSV content to the response.
- The
getCSVContent method is a placeholder for your logic to retrieve and format custom page properties as CSV. You would replace it with the actual implementation based on your requirements.
Remember to adapt the getCSVContent method based on your AEM project's structure and the custom properties you want to export. This example assumes a simple CSV structure for demonstration purposes.