How can I create new CQ Page programmatically?

chung_yonge1684 24-12-2018

Hello Everyone,

Can I ask a question ?

How can I create new CQ Page programmatically?

For example, I want to create new CQ page(menu1, menu2, menu3,…) in Menu folder programmatically.

1653584_pastedImage_0.png

Accepted Solutions (1)

Accepted Solutions (1)

chung_yonge1684 24-12-2018

private void createPage(ResourceResolver resolver, String shipCode) throws Exception {

        String path      =  "/content/test" ;

        String pageName  = "samplepage";

        String pageTitle = "Sample page";

        String template  = "/apps/.../templates/test";

        String renderer  = "/apps/.../components/structure/test";

        Page prodPage = null;

        PageManager pageManager = resolver.adaptTo(PageManager.class);

        prodPage = pageManager.create(path, pageName, template, pageTitle);

        Node pageNode = prodPage.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);

    }