Expand my Community achievements bar.

export custom page properties to csv or excel

Avatar

Level 7

hi,

 

I am in need of exporting custom page properties as csv. Is there any out of the box report tooling or do I need to write a servlet to do this?

 

Thanks,

Sri

3 Replies

Avatar

Level 2

Hi @sreenu539 ,

 

Have you tried the ACS Commons Report Builder feature, where we can create our own query to fetch the results and download the results as CSV?
https://adobe-consulting-services.github.io/acs-aem-commons/features/report-builder/index.html

 

 

Avatar

Community Advisor

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 {

    @reference
    private ResourceResolverService resourceResolverService;

    @Override
    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.

Avatar

Level 10

Hi @sreenu539 ,

 

Please try Bulk Editor https://experienceleague.adobe.com/docs/experience-manager-65/administering/operations/bulk-editor.h...

 

It allows to export information about pages in a tab-separated (.tsv) spreadsheet file

 

For adding custom properties, refer to https://www.dlighthouse.co/2018/11/using-aem-bulk-editor.html 

 

Thanks!