Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Can we create the page programmatically by using editable template

Avatar

Level 2

Hi Team,

      Can anyone let me know whether we can create page using editable template through programmatically. Usually we will create the page programmatically by passing the static template which is under our project but it will not serve purpose right. But if we want to create the page then obviously we have to pass editable template. I got stuck-up there itself can someone help me here please.

Thanks,

Pavan

1 Accepted Solution

Avatar

Correct answer by
Level 3

Hi @MandaPavanKumar 

Yes indeed. You can create page programmatically with editable template too the same way you are creating with static template.

You can use PageManager class from page manager API.

pageManager.create("parent page path", "page name", "template path", "page title");

For more details about page manager API:

Page Manager API Java Doc

 

View solution in original post

2 Replies

Avatar

Correct answer by
Level 3

Hi @MandaPavanKumar 

Yes indeed. You can create page programmatically with editable template too the same way you are creating with static template.

You can use PageManager class from page manager API.

pageManager.create("parent page path", "page name", "template path", "page title");

For more details about page manager API:

Page Manager API Java Doc

 

Avatar

Level 2

YEs it worked and here is the code for that:

package aeminnovations.core.servlets;

import java.io.IOException;
import java.util.Iterator;

import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Session;
import javax.servlet.Servlet;
import javax.servlet.ServletException;

import org.apache.jackrabbit.api.JackrabbitSession;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.json.JSONArray;
import org.json.JSONObject;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.google.gson.Gson;

@Component(service = Servlet.class, property = { Constants.SERVICE_DESCRIPTION + "=Leadership servlet",
		"sling.servlet.methods=" + HttpConstants.METHOD_GET, "sling.servlet.paths=" + "/bin/demo/pagecreate" })
public class CreatePageServlet extends SlingSafeMethodsServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1315366566565122983L;
	private static final String PAGE_TEMPLATE = "/conf/aeminnovations/settings/wcm/templates/content-page";
	private static final String RENDERER = "aeminnovations/components/structure/page";
	@Reference
	private ResourceResolverFactory resolverFactory;
	private ResourceResolver resourceResolver;
	private String pageName;
	private String pageTitle;
	private String path = "/content/aeminnovations/en/leadership";
	private Page prodPage = null;
	private Session session;
	private static final Logger log = LoggerFactory.getLogger(CreatePageServlet.class);

	@Override
	protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
			throws ServletException, IOException {
		try {
			log.info("----------< Executing Query Builder Servlet >----------");
			resourceResolver = request.getResourceResolver();
			session = resourceResolver.adaptTo(Session.class);
			createPages();
			session.save();
			session.refresh(true);

		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			if (session != null) {
				session.logout();
			}
		}
	}

	public void createPages() {
		try {
			PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
			prodPage = pageManager.create(path, pageName, PAGE_TEMPLATE, pageTitle, true);
			Resource resource = resourceResolver.getResource(prodPage.getPath());
			Node pageNode = resource.adaptTo(Node.class);
			Node jcrNode = null;
			if (prodPage.hasContent()) {
				jcrNode = prodPage.getContentResource().adaptTo(Node.class);
			} else {
				jcrNode = pageNode.addNode("jcr:content", "cq:PageContent");
			}
			jcrNode.setProperty("sling:resourceType", RENDERER);
			Node root = session.getNode(prodPage.getPath().toString() + "/jcr:content/root/responsivegrid");
			Node day = root.addNode("biographycomponent", "nt:unstructured");
			day.setProperty("sling:resourceType", "aeminnovations/components/content/biographycomponent");
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			if (session != null) {
				session.logout();
			}
		}
	}
}