Expand my Community achievements bar.

AEM : Create Live Copy Programmatically | AEM Community Blog Seeding




AEM : Create Live Copy Programmatically by AEM Solutions


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.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. */
private RequestResponseFactory requestResponseFactory;
/** The request processor. */
private SlingRequestProcessor requestProcessor;
private void createCopy(ResourceResolver resolver, String payload, final String rootPath, ValueMap vMap,
Map<String, Object> params) throws ServletException, IOException {
Resource res = resolver.getResource(payload);
params.put(CHARSET, StandardCharsets.UTF_8);
params.put(CMD, CMD_LIVE_COPY);
params.put(WCMCommand.SRC_PATH_PARAM, payload);
params.put(WCMCommand.DEST_PATH_PARAM, rootPath);
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, config.getRolloutConfigs());
HttpServletRequest req = requestResponseFactory.createRequest("POST", WCM_COMMAND_ENDPOINT, params);
ByteArrayOutputStream out = new ByteArrayOutputStream();
HttpServletResponse response = requestResponseFactory.createResponse(out);
requestProcessor.processRequest(req, response, resolver);

Read Full Blog

AEM : Create Live Copy Programmatically


Please use this thread to ask the related questions.

Kautuk Sahni

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

0 Replies