Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

Fetch the date property from the page

maryanilak48011
Level 2
Level 2

How can I get the publication date property and the Image property from this page in sling model

maryanilak48011_0-1599753348441.png

 

1 Accepted Solution
raj_mandalapu
Correct answer by
Community Advisor
Community Advisor

You need to handle multiple use cases and conditions

 

1. the direct approach is getting from the current page. inside the sling model, read publication date, and the Image property from the currentPage reference object.

2. if the current page object does not contain these properties, then check for the parent page.

 

But, your requirement is you need to fetch these values into the other page. the other page must know this page path.

1. if it is the parent page, then you can use currentPage.getAbsoluteParent() directly point to that page and read those properties.

2. if it is not the parent page, then the page must be authored somewhere, either in component or page properties of the other page.

3. In your sling model, read that page path and pass it to the getResource("page path");

@PostConstruct
public void init() {
Resource resourceresourceResolver.getResource("page path");
String pDate =resource.getValueMap().get("publicationDate"String.class);
}

 

View solution in original post

7 Replies
Nikhil-Kumar
Community Advisor
Community Advisor

@maryanilak48011 

You can use the resource and use node APIs to get the properties.

Thanks,
Nikhil 

maryanilak48011
Level 2
Level 2
But I need to fetch these values into the other page.
Nikhil-Kumar
Community Advisor
Community Advisor
So invoke this sling model in that page itself. And get the data using node API and return as part of this sling model.
ChitraMadan
Community Advisor
Community Advisor

Hi @maryanilak48011 ,

 

You can do something like below

 

@Getter
@Setter
@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class SampleModel {
 
@SlingObject
private transient ResourceResolver resourceResolver;

@PostConstruct
public void init() {
 
Resource requiredResource = resourceResolver.getResource("path of the page");
String publicationDate =requiredResource.getValueMap().get("publicationDate", String.class);
 
}
Shashi_Mulugu
Community Advisor
Community Advisor

@maryanilak48011 thank you for posting in Communities.  Can you please explain your usecase a bit detailed?

 

I see that you want to fetch publishedDate property from a page into another page?

 

Is it just one property or do you need any other properties (set of properties) ?

 

Is it for direct rendering or any processing logic involved on top of property value?

 

If you have paths of the other pages handy in your current page, you can develop a sling model for the current page resource type and using Pagemanger api or resource api or node api, pass path of the pages in hand and get the properties, if there are multiple properties or any processing logic needed create a sling model for other as well and use adapto feature of resource to fetch values.

raj_mandalapu
Correct answer by
Community Advisor
Community Advisor

You need to handle multiple use cases and conditions

 

1. the direct approach is getting from the current page. inside the sling model, read publication date, and the Image property from the currentPage reference object.

2. if the current page object does not contain these properties, then check for the parent page.

 

But, your requirement is you need to fetch these values into the other page. the other page must know this page path.

1. if it is the parent page, then you can use currentPage.getAbsoluteParent() directly point to that page and read those properties.

2. if it is not the parent page, then the page must be authored somewhere, either in component or page properties of the other page.

3. In your sling model, read that page path and pass it to the getResource("page path");

@PostConstruct
public void init() {
Resource resourceresourceResolver.getResource("page path");
String pDate =resource.getValueMap().get("publicationDate"String.class);
}

 

View solution in original post

maryanilak48011
Level 2
Level 2

As per my requirement, I need to give the page path in the dialog, from that path, I'm fetching all the reference page properties like published date, Image from the reference page which I am giving in the dialog.