Author name and Last published date on a page | Community
Skip to main content
Level 2
April 10, 2023
Solved

Author name and Last published date on a page

  • April 10, 2023
  • 2 replies
  • 1816 views

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.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Shivam153

 

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

2 replies

Shivam153
Level 3
April 10, 2023

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()
ashar02Author
Level 2
April 10, 2023

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

Shivam153
Shivam153Accepted solution
Level 3
April 10, 2023

 

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

ShaileshBassi
Community Advisor
Community Advisor
April 10, 2023

@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

ashar02Author
Level 2
April 11, 2023

@shaileshbassi How to print name and date in html?

VeenaVikraman
Community Advisor
Community Advisor
April 11, 2023

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 ✌