Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session
SOLVED

how to programmatically create a web page for each of thousands of users

Avatar

Level 2

It seems to be a common issue in ecommerce or other types of website to create a web page dynamically for each user or store item. And each page uses unique uuid.html as url. How to achieve this in AEM? Please let me know if there are any samples or manuals which can help.

Thanks and regards,

Yy 

1 Accepted Solution

Avatar

Correct answer by
Level 10

I guess, technically you do not create a new page for every user rather you store users data somewhere and use the same page with different data at run time.

Similarly you don't create new page for every product. You store product data somewhere which you populate in common template at run time.

View solution in original post

4 Replies

Avatar

Administrator

HI

Yes, we can create pages programmatically with AEM.

Option 1:- Create the service to create the page.

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

 

Link: - http://www.albinsblog.com/2014/12/programmatically-create-page-in-cq5.html#.Vhsxy_mqpBc

 

Option 2:- Using curl

There are various way of creating pages using curl command in CQ Some of them are as follows

 

1) curl -u admin:admin –F cmd="createPage" -F label="" -F parentPath="/content/geometrixx/en/company" -F template="/apps/geometrixx/templates/contentpage" -F title="new page" http://localhost:4502/bin/wcmcommand

 

2) curl -u admin:admin -F "jcr:primaryType=cq:Page" -F "jcr:content/jcr:primaryType=cq:PageContent" -F "jcr:content/jcr:title=New Page" -F "jcr:content/sling:resourceType=geometrixx/components/contentpage" http://localhost:4502/content/geometrixx/en/page

 

3) curl --data jcr:primaryType=cq:page --user admin:admin http://<host>:<port>/content/geometrixx/en/toolbar/test3

 

And then

 

curl --data jcr:primaryType=PageContent --user admin:admin http://<host>:<port>/content/geometrixx/en/toolbar/test3/jcr:content

 

Link : http://www.wemblog.com/2011/09/how-to-create-pages-using-curl-command.html

 

I hope this would help you.

 

Thanks and Regards

Kautuk Sahni



Kautuk Sahni

Avatar

Employee

Hi,

you can find out about the different options here[1]. You can have a page per product or you can have a common template that represents each product(the data displayed will be the same for every product.

The unique URL for each product will be based on the path to the product page which is determined during the import process for the product date. 

Regards,

Opkar

[1] https://docs.adobe.com/docs/en/aem/6-0/administer/ecommerce/concepts.html

Avatar

Correct answer by
Level 10

I guess, technically you do not create a new page for every user rather you store users data somewhere and use the same page with different data at run time.

Similarly you don't create new page for every product. You store product data somewhere which you populate in common template at run time.

Avatar

Level 2

Opkar, Kautuk, Edubey,

Thank you very much for replies. If there are thousands or more of products, does that mean the same amount of the nodes would be created under the content/appname folder and same amount of pages got generated in WCM? How could AEM maintain such large amount of nodes and pages? It sounds what Edubey said might solve it if data is stored in external storage system like MySql, but I am not sure if it's the best way.

Yy