Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Is it possible to access pageProperties from a datasource?

Avatar

Level 2

Hi folks. Hope you are doing great.

So this is my problem. I need to dynamically load the options for a select item in a component dialog based on a page property. So I was planning to create a datasource that would read the page properties and then set the select options based on the value of the property. The problem is that if I try to access pageProperties or currentPage it is always null.

Is there a way to actually read page properties from a datasource?

I have my select in the component dialog like this:

<options

           jcr:primaryType="nt:unstructured"

           sling:resourceType="granite/ui/components/coral/foundation/form/select"

           fieldLabel="Options"

           name="./options">

           <datasource

                  jcr:primaryType="nt:unstructured"

                  sling:resourceType="examples/datasources/test"/>

</options>

And my datasource:

<%@page session="false" import="

                  com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap,

                  com.day.cq.commons.inherit.InheritanceValueMap,

                  com.adobe.granite.ui.components.ds.DataSource,

                  com.adobe.granite.ui.components.ds.EmptyDataSource,

                  com.adobe.granite.ui.components.ds.SimpleDataSource,

                  com.adobe.granite.ui.components.ds.ValueMapResource,

                  org.apache.sling.api.resource.Resource,

                  org.apache.sling.api.resource.ResourceMetadata,

                  org.apache.sling.api.resource.ResourceResolver,

                  org.apache.sling.api.resource.ValueMap,

                  org.apache.sling.api.wrappers.ValueMapDecorator,

                  java.util.ArrayList,

                  java.util.HashMap,

                  java.util.Iterator,

                  java.util.List"%>

<%

%><%@taglib prefix="cq" uri="http://www.day.com/taglibs/cq/1.0" %><%

%><cq:defineObjects/><%

     //pageProperties always null here

%>

I'm using AEM 6.4

Thanks

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi,

You can get the Page Property by getting page path and adapt to Node

Example :

  ResourceResolver resolver = request.getResourceResolver();

  String pagePath = request.getRequestPathInfo().getSuffix().replaceAll("/jcr:content/.*$", "");

  Node pageNode = resolver.getResource(pagePath).adaptTo(Node.class);

  String pageTitle= pageNode.getProperty("jcr:title").getString();



Arun Patidar

View solution in original post

2 Replies

Avatar

Level 10

I would use Java and HTL. You can read the page props using Page Manager API.

Using a Page object - you can obtain a lot of information about a given page.

Page ("The Adobe AEM Quickstart and Web Application.")

Then you can put that into a MAP on which a DataSource is based.

See this to learn how to use Java to create a DataSource to populate a Select:

Adobe Experience Manager Help | Using an WCMUsePojo class to populate an Experience Manager Touch UI...

Avatar

Correct answer by
Community Advisor

Hi,

You can get the Page Property by getting page path and adapt to Node

Example :

  ResourceResolver resolver = request.getResourceResolver();

  String pagePath = request.getRequestPathInfo().getSuffix().replaceAll("/jcr:content/.*$", "");

  Node pageNode = resolver.getResource(pagePath).adaptTo(Node.class);

  String pageTitle= pageNode.getProperty("jcr:title").getString();



Arun Patidar