Is there a way to read out the site root page dynamically depending on the site the page is placed in? | Community
Skip to main content
varunmitra
Employee
July 31, 2015

Is there a way to read out the site root page dynamically depending on the site the page is placed in?

  • July 31, 2015
  • 3 replies
  • 2121 views

Yes, you can easily access the website root with the help of Page object. You can directly call getAbsoluteParent method to access the root page at a level of your choice.

Code snippet for the component would look like:

Sightly HTML code snippet:

<div data-sly-use.contentJS="content.js">

    ${contentJS.parentPage.path}

    </div>

Content.js

use(function() {

    return {

      parentPage: currentPage.getAbsoluteParent(1)

     };

});

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

3 replies

kautuk_sahni
Employee
February 10, 2025

@varunmitra Did you find the suggestions helpful? Please let us know if you need more information. If a response worked, kindly mark it as correct for posterity; alternatively, if you found a solution yourself, we’d appreciate it if you could share it with the community. Thank you!

Kautuk Sahni
daniel-strmecki
New Participant
January 18, 2025

Hi @varunmitra,

in Sling Models, if you use adapt from SlingHttpServletRequest then you can:

// Inject the current page @ScriptVariable private Page currentPage; // Get root parent page currentPage.getAbsoluteParent(1)

 

Hope this helps,

Daniel

AmitVishwakarma
New Participant
January 18, 2025

You can dynamically determine the site root page in AEM based on the current page's location. You can achieve this by:

  1. Using AEM's Sling Models: You can create a model that adapts to the current request and retrieves the root page based on the page's path.

  2. Using the JCR Repository: Query the /content structure or use the sling:resourceType property to identify the root page dynamically.

Both approaches let you reference the site root without hardcoding the page path, making it adaptable to different sites.