4 Replies Latest reply on Mar 19, 2013 5:06 AM by creative83

    creating page programatically

    creative83 Level 1

      We can create web page using "Website" section in admin console. I wanted to created page programatically. How can I do it?

        • 1. Re: creating page programatically
          tyge68

          Hi,

           

          You can simply use the PageManager API , check  http://dev.day.com/docs/en/cq/current/javadoc/index.html for more detail on the PageManager

           

          You can get a page manager from a resource resolver object via .adaptTo()

           

          Hope this help.

          • 2. Re: creating page programatically
            creative83 Level 1

             

            Thanks for the reply. Can you please tell me what is adapTo() us used for? I have seen below code:

             

            Resource r = slingRequest.getResourceResolver().getResource(rootPath);

            Node rootNode = r.adaptTo(Node.class);

             

             

            what does above code means? What is rootPath? Can I give any  path here and it will fetch the resource or say that node? what is the meaning of adaptTo and then passing the Node.class?

             

            I created using JcrUtil class. What i did is, I created one sample Page at UI level and then using JcrUtil I copy that page and change the basic property like title, name and page specific component. below is the code I used:

             

            //getting session

            session = currentNode.getSession();

             

            //getting the dummy page which I created at UI level

            Node productNode = currentNode.getParent().getParent().getNode("product");

             

            //copy the dummy page at the same level of "product" page, for that I am using getParent()

            newNode = JcrUtil.copy(productNode,productNode.getParent(),node.getProperty(name).getString());

             

            //Updating the values in tthis new Page for Product Node. IF we don't change then it will be same as old node

            newNode.getNode("jcr:content").setProperty("jcr:title",node.getProperty(name).getString()) ;

            newNode = newNode.getNode("jcr:content").getNode("productForm");

            • 3. Re: creating page programatically
              tyge68 Level 1

              adaptTo is a nice framework that is part of the Apache Sling key concept I would say, if you missed that you need to re-read about Apache Sling in general.

               

              The code you mentioned is simple, it mean it get the resource at the given path and try to transform the resource to another object (a Node in this case).

               

              I have the feeling that you didn't get the basic training of CQ / Apache Sling, it would make sense that you check for that first.

               

              Generally using the JCR api is fine, but in CQ there are some utility classes/services (like the PageManager) that help you to make sure that you create the content with the expected structure, so I would suggest you to use

              that first.

              • 4. Re: creating page programatically
                creative83 Level 1

                Yes you are right, I jsut started learning on my own. I read through the java doc/Geometrixx site and some online content.

                 

                Let me know if you have some good startup training links. Also feel free to give your suggetions for any other thing which you feel I should read. Thanks in advance.

                 

                And I will sure go through the adaptTo and apache Sling.