Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Fetch the date property from the page

Avatar

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

Avatar

Correct answer by
Level 8

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

Avatar

Community Advisor

@maryani 

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

Thanks,
Nikhil 

Avatar

Level 2
But I need to fetch these values into the other page.

Avatar

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.

Avatar

Community Advisor

Hi @maryani ,

 

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);
 
}

Avatar

Community Advisor

@maryani 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.

Avatar

Correct answer by
Level 8

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);
}

 

Avatar

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.