Expand my Community achievements bar.

SOLVED

Is there any way to access page properties if only page path is given?

Avatar

Level 5

I want to access the properties of specific page but I only have its path with me in the sling model.

how can I access the page properties if only the path is given?

 

I have tried using : 

 

@ScriptVariable
private ResourceResolver resourceResolver;
Page rootPage = resourceResolver.getResource(PAGE_PATH).adaptTo(Page.class);

 but I am getting a null pointer exception in this.

Please help me find out how to do it.

 

Thank you

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Follow below lines of code In sling init method

@Model(adaptables = {
        Resource.class }, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL, resourceType = "")
@Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, extensions = ExporterConstants.SLING_MODEL_EXTENSION)
public class YourModel{
@SlingObject
    ResourceResolver resourceResolver;
init(){
Resource resource = resourceResolver.getResource("path");
 PageManager pageManager =  resource.getResourceResolver().adaptTo(PageManager.class);
Page containingPage = pageManager.getContainingPage(resource);
if(Objects.nonNull(containingPage){
 ValueMap pageProperties = containingPage.getProperties();
}
}
}

OR

 PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
Page page = pageManager.getPage(currentPage.getPath());// for current page
Page page = pageManager.getPage("provided path");

View solution in original post

4 Replies

Avatar

Level 7

It should be straight forward:

 

@Model(adaptable=Resource.class)

public class TestClass {

 

@Deleted Account

Resource resource;

 

@PostConstruct

public void init() {

  PageManager pm = resource.getResourceResolver().adaptTo(PageManager.class);

  Page containingPage = pm.getContainingPage (resource);

  ValueMap pageProperties = containingPage.getProperties();

 

}

 

 

}

Avatar

Level 5

Hi @rahul234dabas ,
If resource is null, alternatively you can use

 

PageManager pageManager = resourceResolver.adaptTo(PageManager.class);

Page currentPage = pageManager.getPage("/content/page/en/"); 

 

Avatar

Correct answer by
Community Advisor

Follow below lines of code In sling init method

@Model(adaptables = {
        Resource.class }, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL, resourceType = "")
@Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, extensions = ExporterConstants.SLING_MODEL_EXTENSION)
public class YourModel{
@SlingObject
    ResourceResolver resourceResolver;
init(){
Resource resource = resourceResolver.getResource("path");
 PageManager pageManager =  resource.getResourceResolver().adaptTo(PageManager.class);
Page containingPage = pageManager.getContainingPage(resource);
if(Objects.nonNull(containingPage){
 ValueMap pageProperties = containingPage.getProperties();
}
}
}

OR

 PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
Page page = pageManager.getPage(currentPage.getPath());// for current page
Page page = pageManager.getPage("provided path");

Avatar

Level 5

by changing the annotation from @ScriptVariable to @SlingObject it worked ,

Thank you

May I know what's the difference between two annotations in layman's language?

 

Thank you