4 Replies Latest reply on Apr 18, 2012 4:51 PM by aklimets

    Can I create a teaser programmatically?

    anuumar

      Can I create a teaser programmatically? I also want to create segments and associate my teaser with them.

        • 1. Re: Can I create a teaser programmatically?
          LarsTrieloff

          Yes, you can create teasers, segments and teaser-segment associations programatically using either the JCR API or the Sling REST API. Take the existing teasers and segments as starting points and replicate this content structure and you are all set.

          • 2. Re: Can I create a teaser programmatically?
            anuumar Level 1

            Hi Lars,

             

            Thanks for the reply. Can you please help me with which classes to use. I will really appreciate any sample code.

            • 3. Re: Can I create a teaser programmatically?
              anuumar Level 1

              I was able to create campaigns and teasers successfully by using the "jackrabbit-standalone-2.4.0" jar.

               

              The code:

               

              try {

                                  //Create a connection to the Day CQ repository running on local host

                                  Repository repository = JcrUtils.getRepository("http://localhost:4502/crx/server");

                                  //Create a Session

                                  javax.jcr.Session session = repository.login( new SimpleCredentials("admin", "admin".toCharArray()));

               

               

               

                                  //Create a node that represents the root node

                                  Node root = session.getRootNode();

               

               

               

                Node parent = root.getNode("content/campaigns");

                                            String name = "myCampaign";

                                            String type = "cq:Page";

                                            campaign = JcrUtils.getOrAddNode(parent, name, type);

                                            Node jcrContent = JcrUtils.getOrAddNode(campaign, "jcr:content", "cq:pageContent");

               

                                            Date date  = new Date();

                                            Calendar cal=Calendar.getInstance();

                                            cal.setTime(date);

               

               

                                            jcrContent.setProperty("cq:lastModified", cal);

                                            jcrContent.setProperty("cq:lastModifiedBy", "admin");

                                            jcrContent.setProperty("cq:template", "/libs/cq/personalization/templates/campaign");

                                            jcrContent.setProperty("jcr:title", "My Campaign");

                                            jcrContent.setProperty("sling:resourceType", "cq/personalization/components/campaignpage");

               

               

               

                                  // Save the session changes and log out

                                  session.save();

                                  session.logout();

                                  }

                                  catch(Exception e){

                                  e.printStackTrace();

                                  }

              • 4. Re: Can I create a teaser programmatically?
                aklimets Adobe Employee

                Note that you can also write this code on CQ itself without having to connect remotely via RMI or webdav (which is what JcrUtils.getRepository("http://localhost:4502/crx/server") will do).