Expand my Community achievements bar.

SOLVED

Author name and Last published date on a page

Avatar

Level 3

I have requirements to display Name of author who published the page and published date on page how to fetch those details can anyone suggest.

1 Accepted Solution

Avatar

Correct answer by
Level 4

 

@ashar02 currentPage.getProperties().get("cq:lastReplicated").toString() Use this

View solution in original post

7 Replies

Avatar

Level 4

Hi @ashar02 ,

I am assuming you want to achieve this using a component. So you must have a Sling Model for your component where you can write your logic. Logic will go like this:

  1. Firstly, Inject current Page in your model. 

    @ScriptVariable

    private Page currentPage;

  2. Then, Use this code to get the author of page. currentPage.getProperties().get("jcr:createdBy").toString()

Avatar

Level 3

@Shivam153 Yes I am using component and how to fetch the lastReplicated(publish page) date of a page in the format 4/7/2023.

Avatar

Correct answer by
Level 4

 

@ashar02 currentPage.getProperties().get("cq:lastReplicated").toString() Use this

Avatar

Community Advisor

Write a sling model for your component and read the last published by  property from jcr:content and last:modified. Use simple date format to format the date and pass it to front end. You can also directly print it on html using sightly as page api is available on sightly.

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-to-get-cq-lastmodified...

Avatar

Community Advisor

@ashar02 Code snippet for getting the date and changing to the format.

 

private static final String DATE_FORMAT = "dd MMMM yyyy"

@ScriptVariable
private Page currentPage;

@PostConstruct
void init() {
           ValueMap valMap = currentPage.getProperties();
            if (null == valMap) {
                return;
            }
            String createdBy = valMap.get(JcrConstants.JCR_CREATED_BY, String. class);
            GregorianCalendar publishedDate = valMap.get(NameConstants.PN_PAGE_LAST_PUBLISHED, GregorianCalendar.class);
            String publishedDateString = DateUtils.dateFormat(publishedDate , Date.class), DATE_FORMAT);
}

private static String dateFormat(final GregorianCalendar gregorianCalendarDate, final String dateFormat) {
    final SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
    return sdf.format(gregorianCalendarDate.getTime());
}

 

 

Hope this helps!

Thanks

Avatar

Community Advisor

Hi @ashar02 

 

    I am not sure if you have followed the replies given by other SMEs. One quick question ! Hope you have a sling model for your component.

In the sling model write two getter methods for author and date. 

For e.g something like below 

public String getAuthor() {
return author;
}


public String getPublishedDate() {
return publishedDate;
}

 

In your HTL you can call these methods using 

 

${modelname.author}

 

${modelname.publisheddate}

 

The logic to get the date and format them are already given by other SMEs. Combine all these steps and you should get your required output. 

 

Thanks

Veena ✌