Expand my Community achievements bar.

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

Avatar

Level 3

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)

     };

});

3 Replies

Avatar

Level 7

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.

Avatar

Level 10

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

Avatar

Administrator

@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