AEM : Create Live Copy Programmatically

 



Usually, we create a live copy from a blueprint or a master site, by going to console and use the “Create Live Copy” or “Create Site” options. But there might be scenarios where you need to automate the creation of sites. 

Solution to this is to call the WCMCommand Servlet using Sling Request Processor as there is no API present which can be used directly.

You can either create a service / util to use this piece of code.


 import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.engine.SlingRequestProcessor;
import org.osgi.service.component.annotations.Reference;

import com.day.cq.commons.jcr.JcrConstants;
import com.day.cq.contentsync.handler.util.RequestResponseFactory;
import com.day.cq.wcm.api.commands.WCMCommand;
import com.day.cq.wcm.msm.api.MSMNameConstants;
public class CreateLiveCopy {

   /** The Constant CMD_LIVE_COPY. */
    private static final String CMD_LIVE_COPY = "createLiveCopy";
    /** The Constant CMD. */
    private static final String CMD = "cmd";
    /** The Constant WCM_COMMAND_ENDPOINT. */
    private static final String WCM_COMMAND_ENDPOINT = "/bin/wcmcommand";
    /** The Constant CHARSET. */
    private static final String CHARSET = "_charset_";
    /** The request response factory. */
    @Reference
    private RequestResponseFactory requestResponseFactory;
    /** The request processor. */
    @Reference
    private SlingRequestProcessor requestProcessor;
    private void createCopy(ResourceResolver resolver, String sourcePath, final String destPath, ValueMap vMap, String[] rolloutConfigs) throws ServletException, IOException {
    	Map<String, Object> params = new HashMap<>();
        Resource res = resolver.getResource(sourcePath);
        params.put(CHARSET, StandardCharsets.UTF_8);
        params.put(CMD, CMD_LIVE_COPY);
        params.put(WCMCommand.SRC_PATH_PARAM, sourcePath);
        params.put(WCMCommand.DEST_PATH_PARAM, destPath);
        params.put(WCMCommand.PAGE_TITLE_PARAM, vMap.get(JcrConstants.JCR_TITLE, StringUtils.EMPTY));
        params.put(WCMCommand.PAGE_LABEL_PARAM, vMap.get(JcrConstants.JCR_NAME, StringUtils.EMPTY));
        params.put(MSMNameConstants.PN_ROLLOUT_CONFIGS, rolloutConfigs);
        HttpServletRequest req = requestResponseFactory.createRequest("POST", WCM_COMMAND_ENDPOINT, params);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        HttpServletResponse response = requestResponseFactory.createResponse(out);
        requestProcessor.processRequest(req, response, resolver);
    }
}

Description of the method parameters used:
  • Resource Resolver : required for the WCMCommand Servlet request
  •  srcPath : Path whose live copy needs to be created.
  • destPath : Path under which live copy has to be created.
  • title : srcPath's  page title.
  • label : srcPath's page name.
  • rolloutConfigs : an array of rollout configurations to be included( create a config to add rollout config in your service)

Hope this helps!!

Happy Coding 🙏


If you like my post and find it helpful, you can buy me a coffee.

Comments

  1. Hello Bimmi.

    This doesn't seem to work, after putting everything and running it, we get null pointer exception. can you please check if you missed anything, it would be of great help. Thanks.

    ReplyDelete
    Replies
    1. Hey Saurabh,
      It works for me. Null pointer can be specific to the condition. Can you please explain your issue.
      Thanks

      Delete

Post a Comment