Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.

Programatically dropping components to AEM page

Avatar

Level 2

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

8 Replies

Avatar

Level 3

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>

Avatar

Level 2

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.

Avatar

Level 3

Could you elaborate why it is not possible?

Avatar

Level 2

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. 

Avatar

Level 2

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.

Avatar

Level 3

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.")

Avatar

Level 3

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.