Expand my Community achievements bar.

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

how to publish a page programmatically in cq5

Avatar

Former Community Member

how to publish a page programmatically in cq5

1 Accepted Solution

Avatar

Correct answer by
Level 7

Hi.
It is possible form a servlet to get hold of a replicator by using the @Reference annotation. 
(more info about that here http://dev.day.com/docs/en/cq/current/javadoc/com/day/cq/replication/Replicator.html)

To get hold of a replicator you can use the following code:

@Reference private Replicator replicator;


Then you can use the following code to replicate a page to a publish instance:
 

String path = "PATH OF THE RESOURCE HERE"; Session session = //Get the session fro eg. a the request try { replicator.replicate(session, "ACTIVATE", path); } catch(ReplicationException replicationException) { //Handle the replication exception }

The same goes for any deactivation of a resource.
Hope that will help you.

/Johan

View solution in original post

3 Replies

Avatar

Correct answer by
Level 7

Hi.
It is possible form a servlet to get hold of a replicator by using the @Reference annotation. 
(more info about that here http://dev.day.com/docs/en/cq/current/javadoc/com/day/cq/replication/Replicator.html)

To get hold of a replicator you can use the following code:

@Reference private Replicator replicator;


Then you can use the following code to replicate a page to a publish instance:
 

String path = "PATH OF THE RESOURCE HERE"; Session session = //Get the session fro eg. a the request try { replicator.replicate(session, "ACTIVATE", path); } catch(ReplicationException replicationException) { //Handle the replication exception }

The same goes for any deactivation of a resource.
Hope that will help you.

/Johan

Avatar

Level 10

I would like to just add the below point with Johan

You can use ReplicationActionType to get the different events like ACTIVATE, DEACTIVATE, DELETE, REVERSE (for reverse replicate)

replicator.replicate(session, ReplicationActionType.ACTIVATE, path);

 

~Loki