Expand my Community achievements bar.

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

How can I get the published date of a page in AEM?

Avatar

Level 5

Hello I have a sling java model for component in a page, what I want to do is to take the publish date (this means, the date when you create a page in AEM and click into the publish button)

According to the research that I do, the value of the date of publication of the page of AEM is in the crx and is this one: cq:LastReplicated

This is obtained with the following java code:

 

@inject @Named("jcr:content/cq:lastReplicated") private Date publishedDate

 

I have used it before to obtain the creation date of a page (jcr:created) (that is when you create a page in AEM regardless of whether it is published or not) and the code is the next one:

@inject @Named("jcr:created") private Date createdDate

Unlike when I only obtained the date of creation of the page, here we notice that the date of publication is inside another node (jcr:content)

 

Aaron_Dempwolff_0-1713190619933.png


The code has a conditional that if it doesn’t find the date of publication of the page it uses the date of creation of the page (jcr:created) when we test the code I see that it only uses the date of creation of the page, it doesn’t find the date of publication of the page.

 

Is there any other way to get the publication date in the java sling model?

 

Or the variable that I'm trying to obtain (cq:lastReplicated) is not the indicated one to obtain the date of publication?

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@Aaron_Dempwolff , You can inject current page object in your slingmodel as shown below.

 

@ScriptVariable private Page currentPage;

In the method 

ValueMap pageProperties = currentPage.getProperties();
Date lastReplicated = pageProperties.get(NameConstants.PN_PAGE_LAST_REPLICATED, Date.class)

 Hope this helps!!

View solution in original post

12 Replies

Avatar

Correct answer by
Community Advisor

@Aaron_Dempwolff , You can inject current page object in your slingmodel as shown below.

 

@ScriptVariable private Page currentPage;

In the method 

ValueMap pageProperties = currentPage.getProperties();
Date lastReplicated = pageProperties.get(NameConstants.PN_PAGE_LAST_REPLICATED, Date.class)

 Hope this helps!!

Avatar

Level 5

Hey, It actually works, but for some reason, I tried this into localhost and dev environment and works, but when I deployed into stage (QA) doesn't work, do you know why?

Avatar

Community Advisor

can you please elaborate more, what is the exact issue you are facing?

are you seeing anything in logs?

Avatar

Level 5

what happens is that I have the code elaborated with the part you give me, and it works, but I have tested this code in localhost and in dev environment, and in both it works perfectly, but when I deploy it to stage (QA) it doesn't work, it doesn't bring me the value of the publish date.

I add a try catch block to give me the creation date (the date when you create the page even if is published or not) if don't found the published date of the page

This is how the code looks:

@ScriptVariable private Page currentPage;
@Inject @Named("jcr:created") private Date original_created_page_date;
 
    public String getPublishedDate() {
        String dates = "";

        try {
// Return the published date of the page
            ValueMap pageProperties = currentPage.getProperties();
            Date lastReplicated = pageProperties.get(NameConstants.PN_PAGE_LAST_REPLICATED, Date.class);
            dates = new SimpleDateFormat("yyyy-MM-dd").format(lastReplicated);
        } catch (Exception e) {
// If fails return the creation date of the page
            dates = new SimpleDateFormat("yyyy-MM-dd").format(original_created_page_date);
        }

        return dates;
    }

Avatar

Community Advisor

Can you please update your code using if else condition as using try catch wont work.

public String getPublishedDate() {
String dates = "";
ValueMap pageProperties = currentPage.getProperties();
if(null != pageProperties.get(NameConstants.PN_PAGE_LAST_REPLICATED)) {
Date lastReplicated = pageProperties.get(NameConstants.PN_PAGE_LAST_REPLICATED,
Date.class);
dates = new SimpleDateFormat("yyyy-MM-dd").format(lastReplicated);
}else {
dates = new SimpleDateFormat("yyyy-MM-dd").format(original_created_page_date);
}
}
return dates;
}

Avatar

Level 5

I tried it but still only bringing me the creation page date instead of the published page date

Avatar

Community Advisor

Is this issue happening for all the pages, please validate once. If not,  please make sure it is replicated which you are trying to check is published.
if the issue still persists even after you replicating the page, check in crx whether "cq:lastReplicated" property set or not. If not check your permissions.

 

Avatar

Level 5

Yes it is, this is the crx of how it look, even have the date of today that I make the republish

Aaron_Dempwolff_0-1713285579548.png

 



Avatar

Community Advisor

Try to add logger statements in your code and validate.

Avatar

Level 5

I add logger (info, debug and error) and the only thing that shows is null, is not throwing error and isn't entering in the catch of the try catch block

Avatar

Community Advisor

@Aaron_Dempwolff As I mentioned earlier try catch won't work here as if the property doesn't exist it won't throw any error. So please try to use if else condition to achieve this. And add loggers here as well for tracking.

public String getPublishedDate() {
String dates = "";
ValueMap pageProperties = currentPage.getProperties();
if(null != pageProperties.get(NameConstants.PN_PAGE_LAST_REPLICATED)) {
Date lastReplicated = pageProperties.get(NameConstants.PN_PAGE_LAST_REPLICATED,
Date.class);
dates = new SimpleDateFormat("yyyy-MM-dd").format(lastReplicated);
}else {
dates = new SimpleDateFormat("yyyy-MM-dd").format(original_created_page_date);
}
}
return dates;
}

 

Avatar

Community Advisor

@Aaron_Dempwolff 

You need to handle the scenario where the property is not available or it is null, you can explore the following

https://sling.apache.org/documentation/bundles/models.html#optional-and-required 


Hope this helps



Esteban Bustamante