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
Solved! Go to Solution.
Views
Replies
Total Likes
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");
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();
}
}
Hi @rahul234dabas ,
If resource is null, alternatively you can use
PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
Page currentPage = pageManager.getPage("/content/page/en/");
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");
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
Views
Likes
Replies
Views
Likes
Replies