How to dynamically fetch child pages ?
Hello, I have to fetch the childpages using a pathbrowser dynamically. How to do it without mentioning the page path in the sling model?
Hello, I have to fetch the childpages using a pathbrowser dynamically. How to do it without mentioning the page path in the sling model?
Hi @sanjana12,
Path browser is basically a cq:dialog widget.
example:
<pagepath
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/form/pathbrowser"
fieldLabel="Page"
name="./pagePath"
required="{Boolean}true"
rootPath="/content/project-name"/>
which creates a dialog field widget like this. 
whenever a value is authored in that field, it gets saved as the "name" property mentioned in the dialog.
Now to get this value to your sling model, you can use the following ways:
1. if you sling model is of SlingHttpServletRequest.class, then
@ValueMapValue
private String pagePath;
or
@Inject @Via("resource")
private String pagePath;
2. If your sling model is of resource type, then
@Inject private String pagePath;
Once you get your path from the path browser, you now require a Resource Resolver, to track down to your resource and get the child pages.
you can use the way how @ksh_ingole7 mentioned, but I would like to improvise the injection of resource resolver there
@SlingObject ResourceResolver resourceResolver;
Now here comes the question what is a resource resolver?
Answer:
In simple words, a Resource Resolver is a basically an interface between the objects in the repository (Sling considers them as resources) and the code you write. Since the interface api helps to access the resources whilst having all the access related factors (resolution), it is hence named Resource Resolver.
Correct syntax of using Resource resolver:
In a Sling Servlet, resource resolver is available to the request processing servlet through the SlingHttpServletRequest.getResourceResolver() method.
In a Sling Model, you can have the instance of resource resolver through the injector specific annotation "@SlingObject"
@SlingObject
private ResourceResolver resourceResolver;
In other way, you can also get the resource resolver through ResourceResolverFactory.
@Reference private transient ResourceResolverFactory resourceResolverFactory; ResourceResolver resourceResolver = resourceResolverFactory.getServiceResourceResolver(ResourceResolverFactory.SUBSERVICE, "service-user-name");
In the above code, the parameters passed in the getServiceResourceResolver are sub service constant and service-user-name, which is the system user id who have access to the resources.
to know more on how to create a system/service user, please refer this official document or follow this simple video.
I hope this helps.
Thank you.
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.