Expand my Community achievements bar.

SOLVED

How can I create new CQ Page programmatically?

Avatar

Level 4

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

1 Accepted Solution

Avatar

Correct answer by
Level 4

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);

    }

View solution in original post

4 Replies

Avatar

Correct answer by
Level 4

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);

    }

Avatar

Level 3

Hi Ratna Kumar,

    The link which you had mentioned is showing 404 error. Could you please help me with the correct link please

Thanks,

Pavan