How to programmatically retrieve URL of Page and/or Resource? | Community
Skip to main content
Level 4
October 30, 2022
Solved

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

  • October 30, 2022
  • 2 replies
  • 885 views

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 = ...?

 

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 SantoshSai

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

2 replies

SantoshSai
Community Advisor
SantoshSaiCommunity AdvisorAccepted solution
Community Advisor
October 30, 2022

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

Santosh Sai
TarunKumar
Community Advisor
Community Advisor
October 31, 2022

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!