Fetch site page path in AEM component | Community
Skip to main content
stiegjo
Level 2
April 18, 2024
Solved

Fetch site page path in AEM component

  • April 18, 2024
  • 2 replies
  • 1866 views

I'm trying to fetch the site page path that my AEM component resides in. However when I try it with this following method, I get the top-level path returned (i.e. /content/my-site/us/en) instead of the actual page path. Any suggestions on how to adjust this method? Thanks.

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

 

 

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 stiegjo

To get this to work, I ended up using a utility method that retrieves the current page from a SlingHttpServletRequest. It does this by checking several possible sources for the page information:


1. It first checks if the suffix resource of the request is not null and if its path starts with CONTENT_ROOT. If so, it uses a getContainingPage method to get the page containing the suffix resource.
2. If the first check doesn't yield a page, it then checks if there is a parameter named ITEM_PARAMETER in the request. If there is, it uses the PageManager.getContainingPage method to get the page containing the resource specified by the ITEM_PARAMETER.
3. If the second check doesn't yield a page, it then checks the REFERER header of the request. If the header is not blank, it extracts the page path from the referer URL and uses the PageManager.getPage method to get the page at that path. 

2 replies

Harwinder-singh
Community Advisor
Community Advisor
April 18, 2024

@stiegjo You can inject the current page object in your sling model. 

 

Something like this should do : 

 

@ScriptVariable
protected Page currentPage;

 

public string getCurrenPath() {

String currentPagePath = currentPage.getPath();

return currentPagePath;

}

stiegjo
stiegjoAuthor
Level 2
April 18, 2024

I'm seeing the same results:

 

 /content/my-site/us/en

Harwinder-singh
Community Advisor
Community Advisor
April 18, 2024

@stiegjo , then please check the request object and the resource object that you are deriving out of the request.

Can you please share the sling model code ?

 I am suspecting your problem is similar to this one - https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/aem-6-3-get-the-current-page-from-java-model-class/m-p/238753

 

 

stiegjo
stiegjoAuthorAccepted solution
Level 2
April 18, 2024

To get this to work, I ended up using a utility method that retrieves the current page from a SlingHttpServletRequest. It does this by checking several possible sources for the page information:


1. It first checks if the suffix resource of the request is not null and if its path starts with CONTENT_ROOT. If so, it uses a getContainingPage method to get the page containing the suffix resource.
2. If the first check doesn't yield a page, it then checks if there is a parameter named ITEM_PARAMETER in the request. If there is, it uses the PageManager.getContainingPage method to get the page containing the resource specified by the ITEM_PARAMETER.
3. If the second check doesn't yield a page, it then checks the REFERER header of the request. If the header is not blank, it extracts the page path from the referer URL and uses the PageManager.getPage method to get the page at that path.