how to get retrieve a page using PageManager.getPath | Community
Skip to main content
jayv25585659
Level 8
January 5, 2018
Solved

how to get retrieve a page using PageManager.getPath

  • January 5, 2018
  • 4 replies
  • 27896 views

so I have the following code snippet

import com.day.cq.wcm.api.Page;

import com.day.cq.wcm.api.PageManager;

...

...

//more unrelated code here

...

...

Page currentPage = pageManager.getPage("/content/data/fishes/greatwhite");

log.error(currentPage.getTitle());

from what I understand (PageManager ("The Adobe AEM Quickstart and Web Application.") ), getPath requires a String input.

And from the code snippet I have provided above, the path I provided is valid. I checked this via CRX/DE.

I've looked at various examples around the net and I cannot find examples where the string supplied to getPath is a string literal. It's always a variable.

I'm just wondering what sort of path is required.

When I debugged my code using Eclipse, currentPage is neither empty or null. like it's undefined. so perhaps I'm using it wrong.

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 edubey

Documentation is correct, it has to be the path to the page in repository (string)

Use the sample working code below, make sure you get pageManager object before you do getTitle

        ResourceResolver resourceResolver = request.getResourceResolver();

        PageManager pageManager = resourceResolver.adaptTo(PageManager.class);

        Page currentPage = pageManager.getPage("/content/page/en/"); 

        response.getWriter().println("Hello from " + currentPage.getTitle());

Code depicts the use case in a servlet to get resource resolver need for pagemanager, though you can refer to this doc if you are not use in servlet to get access to resourceresolver

https://sling.apache.org/documentation/tutorials-how-tos/getting-resources-and-properties-in-sling.html

4 replies

edubey
edubeyAccepted solution
Level 10
January 5, 2018

Documentation is correct, it has to be the path to the page in repository (string)

Use the sample working code below, make sure you get pageManager object before you do getTitle

        ResourceResolver resourceResolver = request.getResourceResolver();

        PageManager pageManager = resourceResolver.adaptTo(PageManager.class);

        Page currentPage = pageManager.getPage("/content/page/en/"); 

        response.getWriter().println("Hello from " + currentPage.getTitle());

Code depicts the use case in a servlet to get resource resolver need for pagemanager, though you can refer to this doc if you are not use in servlet to get access to resourceresolver

https://sling.apache.org/documentation/tutorials-how-tos/getting-resources-and-properties-in-sling.html

Feike_Visser1
Adobe Employee
Adobe Employee
January 5, 2018

You can also do first a getResource(), to make you are working an existing resource.

Then do resource.getPath() as input to getPage()

ebin_Aby
Level 2
August 19, 2019

Q : The pageManager work for me in Author but does not work in publisher. Seems like it is unable to get the data. Have anyone faced this issue ?  

Gaurav-Behl
Level 10
August 20, 2019

Do you mean that anonymous user doesn't work for this use case but when you login with admin on pubs it works fine?

If yes, use service user and assign appropriate permissions on specific path hierarchy. Modify the code as applicable.

Service Users in AEM

http://www.aemcq5tutorials.com/tutorials/create-system-user-in-aem/