Expand my Community achievements bar.

Unpublish AEM page from code

Avatar

Community Advisor

Hello,

I have a requirement to unpublish a page from AEM and move that unpublished page to another location.

From an author service, I did this,

String pagePath = hit.getPath();
replicator.replicate(session, ReplicationActionType.DEACTIVATE, pagePath); // Unpublish the Page
pageMoveService.movePages(pageManager, deletedDestinationPath, pagePath, deletedPageTitleSuffix); // Move that page in different location

In another Preprocessor, which is written for general page publish/unpublish,

String eventType = replicationAction.getType().getName();
String replicationPath = replicationAction.getPath();
if (!eventType.equals("Activate")) {
    Resource resource = resourceResolver.getResource(replicationPath + "/jcr:content");
    // Some business logic
}

This works fine, but the issue is, when I request to deactivate the page sometimes it's not being Unpublished instantly. That's why the move operation moved the page and I got unexpected data in the preprocessor.

 

Requirement:

I need to make sure the page is unpublished, then I need to execute the move operation. How to do that?

 

Environment: AMS

7 Replies

Avatar

Level 4

Hi @Sady_Rifat ,

 

You can write a EventHandler and register the handler for Replication deactivation. Once the replication deactivation triggers from your OSGI service and once the replication agent deactivates the content then your event handler will execute. Here in the handler you can do page move operation.

 

 

Thanks

Ramesh

Avatar

Community Advisor

Hello @Uppari_Ramesh, yes I already have replicationAction in my codebase that I already mentioned.
But I need to only move some specific pages based on condition.
for example, if I write the move operation in replicationAction then, if an Author unpublish any page, it will be moved.

Avatar

Level 4

Hi @Sady_Rifat You can even validate the specific condition in Event handler OR if you have path based validation then you define the handler with filter of paths as well. You will get the page path in handler and do your validation and based on that move pages.

Avatar

Community Advisor

Hi @Sady_Rifat 

I think If the page was published before the move, the old path is automatically unpublished. Can you try just move method  from PageManager Interface



Arun Patidar

Avatar

Community Advisor

@arunpatidar actually I am working with Unpublish a page via pageManger API.

Avatar

Level 8

Hi @Sady_Rifat 

After you initiated the deactivation, there is no feedback to know when the system actually finished. What you can do instead is to create and start an ad-hoc Sling job that monitors the page path and checks in loop the value of cq:lastReplicationAction property until it changes from Activate to Deactivate.

Tethich_0-1736844720640.png

Tethich_2-1736844744672.png


This is a nice article on how to use Sling Jobs in case you haven't so far: https://medium.com/@toimrank/aem-sling-jobs-c4ebbb849d68
And in order to control the number of retries and teh delay between two retries (I believe you will need to set this based on your guts, on how much time usually takes for a page to get deativated by your system), you will need to set these two properties for the job: event.job.retrydelay and event.job.retries.

 

Tethich_5-1736845816168.png


More about these properties in here https://github.com/apache/sling-org-apache-sling-event-api/blob/master/src/main/java/org/apache/slin...