Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.

Retrive image from the page properties

Avatar

Level 4

Hi 
i want to have images that i have uploaded in my child page in page properties to be in my parent page
Below is code where i want title , desctiption and image from my child pages that i have added in page properties while child page creation.

here is my code snippet:

  List<ExistingPagesModel> pagesList = new ArrayList<>();
        Resource parentResource = resourceResolver.getResource("/content/us/en/home/test");

        if (parentResource != null) {
            Iterator<Resource> childResources = parentResource.listChildren();
            log.info("childResources: {}");

            while (childResources.hasNext()) {
                Resource childResource = childResources.next();
                log.info("Processing child resource: {}", childResource.getPath());

                // Get property values from child resource
                childTitle = childResource.getValueMap().get("jcr:content/jcr:title", String.class);
                childDescription = childResource.getValueMap().get("jcr:content/jcr:description", String.class);
                String childImage = childResource.getValueMap().get("cq:featuredimage/fileReference", String.class);

                // childImage = getChildImageFromResource(childResource);

                log.info("Child Title: {}", childTitle);
                log.info("Child Description: {}", childDescription);
                log.info("childImage: {}", childImage);
                if (childTitle != null) {
                    // Create an ExistingPagesModel object for each child page
                    ExistingPagesModel pageInfo = childResource.adaptTo(ExistingPagesModel.class);
                    if (pageInfo != null) {
                        pageInfo.setChildTitle(childTitle);
                        pageInfo.setChildDescription(childDescription);
                        pageInfo.setChildImage(childImage);
                        pagesList.add(pageInfo);
                    }
                }
            }
        }
        log.info("pageInfo: {}", pagesList);
        return pagesList;
    }



title and description are working but in childImage i am getting null can someone help me with that?




2 Replies

Avatar

Community Advisor

Hi @tatrived Can you share the content.xml file screenshot how the content is saved on your page. As the property name has to be matched while retrieving the filereference.

Thanks,

Shailesh