Normally, when we want to create a live copy from a blueprint or a master site, we go to the Sites Console and use the “Create Live Copy” or “Create Site” button. But sometimes there might be a need to automate the creation of sites. For eg:, there might be a requirement to create multi-tenant sites using the blueprint or master site automatically or programmatically, based on some inputs or criteria. Currently there is no Java API which can be directly used to create the live copy.
Here is the solution: Call the WCMCommand Servlet using Sling Request Processor.
Create a custom OSGi Service
- Create a custom OSGi Service which can be called from any other class.
- Declare a method which accepts the arguments required to create a live copy -
a. Resource Resolver : required for the WCMCommand Servlet request
b. srcPath : the master path from which live copy has to be created
c. destPath : the path under which live copy has to be created
d. title : the livecopy root page title
e. label : the livecopy root page name
f. rolloutConfigs : an array of rollout configurations to be included
Call the WCMCommand Servlet (Service Implementation)
- Create a request object using RequestResponseFactory. We need to create a POST request to “/bin/wcmcommand”, along with the parameters which are required. The command (“cmd”) to create live copy is “createLiveCopy”.
- Create a response object using RequestResponseFactory.
- Pass the request, response and resource resolver objects to the SlingRequestProcessor#processRequest method. This will trigger the POST request and creates the live copy.
Testing the Service
Let’s test by creating a live copy. Here’s a sample servlet which uses the service to create a live copy.
- The resource resolver which is used, should have the permissions to create a live copy.
- It is recommended to validate the input to the “createLiveCopy” method, to avoid any unintended operations.
- It is recommended to do error handling, to provide a meaningful error message back to the service, which calls the MSMService.
- Sling Request Processor — http://www.nateyolles.com/blog/2015/10/get-rendered-html-for-an-aem-resource-or-component
Any questions or suggestions? Please feel free to comment.