Is it possible to access pageProperties from a datasource? | Community
Skip to main content
Level 2
October 11, 2018
Solved

Is it possible to access pageProperties from a datasource?

  • October 11, 2018
  • 2 replies
  • 3386 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by arunpatidar

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();

2 replies

smacdonald2008
Level 10
October 11, 2018

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 Select Field

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
October 11, 2018

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