Expand my Community achievements bar.

SOLVED

how to get retrieve a page using PageManager.getPath

Avatar

Level 9

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

1 Accepted Solution

Avatar

Correct answer by
Level 10

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.h...

View solution in original post

4 Replies

Avatar

Correct answer by
Level 10

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.h...

Avatar

Employee

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

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

Avatar

Level 3

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 ?  

Avatar

Level 10

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/