Expand my Community achievements bar.

Radically easy to access on brand approved content for distribution and omnichannel performant delivery. AEM Assets Content Hub and Dynamic Media with OpenAPI capabilities is now GA.
SOLVED

How to programmatically retrieve URL of Page and/or Resource?

Avatar

Level 5

I have a Resource that represents a page. What is the proper way to get the URL for this page?  These resources are coming from search Hit objects.

 

// in my case, paths are coming from search Hit objects
Resource pageResource = resourceResolver.getResource(path);
Page page = pageResource.adaptTo(Page.class);

String url = ...?

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @dylanmccurry ,

Try the below code snippet might help. Here I am getting Resource and ResourceResolver from request.

PageManager pageManager = request.getResourceResolver().adaptTo(PageManager.class);
Page currentPage = pageManager.getContainingPage(request.getResource());
String pagePath = currentPage.getPath();

 Regards,

Santosh

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

Hi @dylanmccurry ,

Try the below code snippet might help. Here I am getting Resource and ResourceResolver from request.

PageManager pageManager = request.getResourceResolver().adaptTo(PageManager.class);
Page currentPage = pageManager.getContainingPage(request.getResource());
String pagePath = currentPage.getPath();

 Regards,

Santosh

Avatar

Community Advisor

HI @dylanmccurry 

You can use below lines of code:

Resource pageResource = resourceResolver.getResource(path);
String pagePath = pageResource.getPath.replace("/jcr:content", StringUtils.EMPTY);

Hope this helps!