Pages

Sunday, December 14, 2014

how to create the page through Java API in Adobe Experience Manager(AEM)

how to create the page through Java API in Adobe Experience Manager(AEM)

This post will explain how to create a page through Java API in Adobe Experience Manager(AEM)

Create the API service

This service create a sample page in Adobe Experience Manager(AEM).

import javax.jcr.Node;
import javax.jcr.Session;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.osgi.service.component.ComponentContext;

import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;

@Component(immediate = true, label = "Create Page Service", description = "Create Sample Page", metatype = true)
@Service(value = CreateSamplePage.class )
public class CreateSamplePage   {

@Reference
private ResourceResolverFactory resolverFactory;
private ResourceResolver resourceResolver;
 
private void createPage() throws Exception {
String path="/content/poc";
String pageName="samplePage";
String pageTitle="Sample Page";
String template="/apps/geometrixx/templates/homepage";
String renderer="geometrixx/components/homepage";

this.resourceResolver = this.resolverFactory.getAdministrativeResourceResolver(null);
    Page prodPage = null;
    Session session = this.resourceResolver.adaptTo(Session.class);
    try {
    if (session != null) {

    // Create Page    
    PageManager pageManager = this.resourceResolver.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);
     
     
         Node parNode = jcrNode.addNode("par");
         parNode.setProperty("sling:resourceType", "foundation/components/parsys");

 Node textNode = parNode.addNode("text");
 textNode.setProperty("sling:resourceType", "foundation/components/text");
 textNode.setProperty("textIsRich", "true");
 textNode.setProperty("text", "Test page");

session.save();
        session.refresh(true);
 }
       
} catch (Exception e) {
throw e;

   }    
}










8 comments:

  1. After creating a page how to publishing a page programmatically in cq5

    ReplyDelete
    Replies
    1. Refer the following blog - http://www.practicalaem.com/2016/05/04/activate-or-publish-a-page-programmatically/

      Delete
    2. Hi Albin
      I have seen your refer link it's very nice .but actually i want to activate a page through @Test case code or java program please help on this one .

      Delete
    3. You can refer the above link that will guide you to activate the page through java.

      Delete
    4. Hi Albin you are great
      Albin Actually I need to create a node using the front end and java program
      In front end I will capture the value of section name ,xtype, fieldname.. how can we do that

      Delete
  2. i am getting an exception "javax.jcr.ItemExistsException: jcr:content"

    ReplyDelete
  3. Very good article!!

    ReplyDelete
  4. Hi, Is it possible to create page Without ResourceResolver(as an external user..third party user- right user credentials) Thanks!

    ReplyDelete