Expand my Community achievements bar.

Fetch Page properties in Sling Model

Avatar

Level 2

Hi Everyone,

I have a page created using Editable template.
I have a component which is placed in Template.

I have written a Sling model to fetch page properties by using resource.But not able to fetch page properties.It is fetching template properties.
Can Any one help me how to fetch page properties


Thanks
Bhanu Prakash

10 Replies

Avatar

Employee Advisor

It should be straight forward:

@Model(adaptable=Resource.class)

public class myClass {

@Self

Resource resource;

@PostConstruct

public void init() {

  PageManager pm = resource.getResourceResolver().adaptTo(PageManager.class);

  Page containingPage = pm.getContainingPage (resource);

  ValueMap pageProperties = containingPage.getProperties();

}

}

Avatar

Level 2

Thanks Joerg for the input.

If the component is rendered in Page not from template then above code is able to fetch page properties..
But In my case the Model class is called from component which is placed in Editable Template.
For this scenario I am not able to fetch page properties with the help of above sample code.

Avatar

Employee Advisor

Hi,

I don't understand what you mean with "is rendered in Page not from template". The code above should work in any kind of component, which is part of a page; and it should not matter if this page has been created based on a static template or an editable template.

Can you tell where the above code is failing in your case? What's the behaviour you observe? Any log message or exception?

Jörg

Avatar

Level 1

What changes to make if I want only to display one particular page property field??

Avatar

Level 1

Even I am having the same situation and I was not able to read page properties on the which is authored on the template.

Avatar

Community Advisor

Hi Bhanu,

First you should ask few questions to yourself :

1. If page is not created how page properties can be populated in template editor?

2. If page is available, then how template would know which page properties it should return because template is common.

The sling Model suggested by Jorg is working fine but adapting request resource and returning properties of resource(template in case of template editor and page properties in case of page)



Arun Patidar

Avatar

Level 2

Hi Arun,

I will explain the scenrio in detailed.

1. I have an editable template(template1) to create pages.

2. I have create a page(page1) by using above template.

3.I have a component (Component1) to call sling model and fetch page properties.

Scenario1: If the component1 is authored in template and then tried to load the page. I am able see template properties as response not page properties.

Logs for scenrio1:

Page:::JcrPropertyMap [node=Node[NodeDelegate{tree=/conf/test-kds/settings/wcm/templates/pdp/structure/jcr:content: { jcr:primaryType = cq:PageContent, jcr:mixinTypes = [mix:versionable], jcr:createdBy = anusha.addepalli, cq:lastReplicationAction = Activate, jcr:versionHistory = 369132ce-20fd-4a47-b362-fe4b619dbf56, cq:template = /conf/test-kds/settings/wcm/templates/pdp, cq:lastReplicatedBy = anusha.addepalli, jcr:predecessors = [cbd664c4-81df-4afe-a0a5-597b3d7d78dc], jcr:created = 2019-07-23T07:35:47.923-05:00, cq:lastReplicated = 2019-05-22T00:46:40.936-05:00, cq:lastModified = 2019-07-31T06:54:18.349-05:00, jcr:baseVersion = cbd664c4-81df-4afe-a0a5-597b3d7d78dc, jcr:isCheckedOut = true, cq:deviceGroups = [/etc/mobile/groups/responsive], jcr:uuid = caf897a4-a22c-45db-9d8e-c673d86f2fbe, sling:resourceType = test/test-kds/components/structure/page, cq:lastModifiedBy = anusha.addepalli, root = { ... }, cq:responsive = { ... }}}], values={cq:template=/conf/test-kds/settings/wcm/templates/pdp, jcr:primaryType=cq:PageContent, jcr:mixinTypes=[Ljava.lang.String;@65c10fbb, jcr:createdBy=anusha.addepalli, cq:lastReplicationAction=Activate, jcr:versionHistory=369132ce-20fd-4a47-b362-fe4b619dbf56, cq:lastReplicatedBy=anusha.addepalli, jcr:predecessors=[Ljava.lang.String;@68d3d17b, jcr:created=java.util.GregorianCalendar[time=1563885347923,areFieldsSet=true,areAllFieldsSet=true,lenient=false,zone=sun.util.calendar.ZoneInfo[id="GMT-05:00",offset=-18000000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2019,MONTH=6,WEEK_OF_YEAR=30,WEEK_OF_MONTH=4,DAY_OF_MONTH=23,DAY_OF_YEAR=204,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=4,AM_PM=0,HOUR=7,HOUR_OF_DAY=7,MINUTE=35,SECOND=47,MILLISECOND=923,ZONE_OFFSET=-18000000,DST_OFFSET=0], cq:lastReplicated=java.util.GregorianCalendar[time=1558504000936,areFieldsSet=true,areAllFieldsSet=true,lenient=false,zone=sun.util.calendar.ZoneInfo[id="GMT-05:00",offset=-18000000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2019,MONTH=4,WEEK_OF_YEAR=21,WEEK_OF_MONTH=4,DAY_OF_MONTH=22,DAY_OF_YEAR=142,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=4,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=46,SECOND=40,MILLISECOND=936,ZONE_OFFSET=-18000000,DST_OFFSET=0], cq:lastModified=java.util.GregorianCalendar[time=1564574058349,areFieldsSet=true,areAllFieldsSet=true,lenient=false,zone=sun.util.calendar.ZoneInfo[id="GMT-05:00",offset=-18000000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2019,MONTH=6,WEEK_OF_YEAR=31,WEEK_OF_MONTH=5,DAY_OF_MONTH=31,DAY_OF_YEAR=212,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=5,AM_PM=0,HOUR=6,HOUR_OF_DAY=6,MINUTE=54,SECOND=18,MILLISECOND=349,ZONE_OFFSET=-18000000,DST_OFFSET=0], jcr:baseVersion=cbd664c4-81df-4afe-a0a5-597b3d7d78dc, jcr:isCheckedOut=true, cq:deviceGroups=[Ljava.lang.String;@c2634ff, jcr:uuid=caf897a4-a22c-45db-9d8e-c673d86f2fbe, sling:resourceType=test/test-kds/components/structure/page, cq:lastModifiedBy=anusha.addepalli}]

Scenario2: If the component is drag and drop in the created page(page1) and then we are able to see page properties as response

Avatar

Community Advisor

ok, then tell me

Scenario1: If the component1 is authored in template and then tried to load the page. I am able see template properties as response not page properties.

What are you expecting here page1 properties, if there is page2 then?

There is no one to one mapping between template and page rather 1 to M



Arun Patidar

Avatar

Level 2

Component is authored in template so if create a new page i can able see page along with components authored in template.

In component we are calling sling model to return page properties.

if page1 is opened then sling model will be called and  page1 properties should return and same for other pages also

Avatar

Community Advisor

Hi,

If you will open the page in page editor, you will see the page properties

e.g.

http://localhost:4504/editor.html/content/AEM64App/en.html

If you will open the template in template editor, you will see the template properties

e.g.

http://localhost:4504/editor.html/conf/AEM64App/settings/wcm/templates/dy-a-b/structure.html

This is correct behavior.



Arun Patidar