Programatically dropping components to AEM page | Community
Skip to main content
Level 2
February 16, 2018
Question

Programatically dropping components to AEM page

  • February 16, 2018
  • 8 replies
  • 5624 views

HI All,

I want to create page and drop components along with its dialog values using java code. Is there any way to invoke any API with component field values to drop component in page ?

Currently I am creating jcr node using sling api but the disadvantage I see in that approach is , when we have many components and each component design is different, then I need to change the node creation logic. So is there any API to directly save component to page?

Thanks,

Vidhu

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

8 replies

Level 3
February 16, 2018

Why don't you add the components to the template?

Example:

<?xml version="1.0" encoding="UTF-8"?>

<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0"

          xmlns:jcr="http://www.jcp.org/jcr/1.0"

          jcr:description="Template with focus on content."

          jcr:primaryType="cq:Template"

          jcr:title="Custom Page"

          ranking="{Long}200">

    <jcr:content

            jcr:primaryType="cq:PageContent"

            sling:resourceType="<YOUR_SITE>/components/structure/page">

        <par

                jcr:primaryType="nt:unstructured"

                sling:resourceType="wcm/foundation/components/parsys">

            <custom-component

                    jcr:primaryType="nt:unstructured"

                    sling:resourceType="<YOUR_SITE>/components/content/custom-component"

                    property="value"/>

        </par>

    </jcr:content>

</jcr:root>

Level 2
February 17, 2018

Thanks,

It is not possible as per our architecture. I am just trying to automate content author functionality to create pages using java. Components and templates are already developed and cant be redesigned.

Level 3
February 19, 2018

Could you elaborate why it is not possible?

Level 2
February 19, 2018

We are doing content migration from our legacy site to our new aem site. So we are taking content from inventory and are dynamically mapping content to AEM components using java code. We have our components development completed and now we need to automate the creation of pages. So we analyse components required for a particular page and then create page using java code with all the components.

I hope you understood our context. 

Level 2
February 19, 2018

We are doing content migration from our legacy site to our new aem site. So we are taking content from inventory and are dynamically mapping content to AEM components using java code. We have our components development completed and now we need to automate the creation of pages. So we analyse components required for a particular page and then create page using java code with all the components.

I hope you understood our context.

Hemant_arora
Level 8
February 19, 2018
Level 3
February 19, 2018

Yes, I understand what the case is here. Still, I would recommend to add the components to the template. If you do this, you can call the PageManager to create a page with your specific template for all of the pages on your legacy system. This way, the new created page will have the components available to which you mapped your content on the legacy system.

For more info on the PageManager, please look at

PageManager ("The Adobe AEM Quickstart and Web Application.")

Julio_Tobar
Level 3
February 19, 2018

vidhumitram53839032 , let me see if I fully understand your requirement:

Legacy System

  1. JCR based system with content for pages

New System

  1. Templates already created
  2. Content components already created

Requirement

  1. Using java code you must
  • create PAGES
  • Add legacy content to the newly created pages
  • Authors should be able to go into the pages and use the component's dialogs to edit the migrated content

If that's the case, then you're on the right path, continue to use Sling APIs for creating the JCR nodes  in the new system (don't forget to add sling:resourcePath to map to the Component) and in case of node's properties maybe Create a generic Method to read a ValueMap<String, Object> from legacy Node, traverse it and create the properties in new system.