Create the entire page incuding subnodes under jcr:content using a JSON file | Community
Skip to main content
Level 3
April 28, 2023

Create the entire page incuding subnodes under jcr:content using a JSON file

  • April 28, 2023
  • 7 replies
  • 1698 views

Hi All,

 

I have a requirement to create the AEM page and its sub-nodes from the JSON file programmatically which follows the same structure as the one generated from <path to the page>.infinity.json.

Any suggestions on this will be helpful?

 

Thanks,

Sagar

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

7 replies

DPrakashRaj
Community Advisor
Community Advisor
April 28, 2023

yes, you should be able to create a page programmatically in aem using the page manager api.

See https://www.albinsblog.com/2014/12/programmatically-create-page-in-cq5.html#.ZEwxlnbMI2w for reference

Saravanan_Dharmaraj
Community Advisor
Community Advisor
April 28, 2023

@sagarkmr0915 Have you checked Sling Post Servlet examples to create page like this one below

https://blogs.perficient.com/2021/08/16/loading-json-content-into-aem/ 

Level 3
April 28, 2023

@saravanan_dharmaraj  - Did try this approach. It works for most part but doesn't update protected properties like jcr:createdBy. I know those are non modifiable, but in any way this too can be updated programmatically than just auto update the jcr:createdBy to the logged in userid?

DPrakashRaj
Community Advisor
Community Advisor
April 28, 2023

No you can’t. Those are non- modifiable property

Shiv_Prakash_Patel
Community Advisor
Community Advisor
April 29, 2023

Hi @sagarkmr0915 ,

Import operation in Sling Post servlet can deal with you requirement as suggested by @saravanan_dharmaraj .

You are also looking to update jcr:createdBy with the logged-in user. This can be achieved by passing the user ID and password in the curl command as below in shell script.

You can also run curl command in AEM Servlet Class as  - https://unlocklearning.in/package-migration-via-approval-workflow-process/ 

#!/bin/bash 
curl -u shiv:12345 \
-F":name=pagewithjson" \
-F":contentFile=@fullpagedata.json" \
-F":operation=import" \
-F":contentType=json" \
-F":replace=true" \
-F":replaceProperties=true" \
http://localhost:4502/content/learning/us/en/

Input json data

{
"jcr:primaryType": "cq:Page",
"jcr:content": {
"jcr:primaryType": "cq:PageContent",
"jcr:title": "samplePage",
"cq:template": "/conf/learning/settings/wcm/templates/page-content",
"sling:resourceType": "learning/components/page",
"root": {
"jcr:primaryType": "nt:unstructured",
"layout": "responsiveGrid",
"sling:resourceType": "learning/components/container",
"container": {
"jcr:primaryType": "nt:unstructured",
"layout": "responsiveGrid",
"sling:resourceType": "learning/components/container",
"title": {
"jcr:primaryType": "nt:unstructured",
"sling:resourceType": "learning/components/title"
},
"container": {
"jcr:primaryType": "nt:unstructured",
"layout": "responsiveGrid",
"sling:resourceType": "learning/components/container",
"text": {
"jcr:primaryType": "nt:unstructured",
"text": "<p>RAM</p>\r\n",
"sling:resourceType": "learning/components/text",
"textIsRich": "true"
}
}
}
}
}
}

Created page json

{
"jcr:primaryType": "cq:Page",
"jcr:createdBy": "shiv",
"jcr:created": "Sat Apr 29 2023 11:55:37 GMT+0530",
"jcr:content": {
"jcr:primaryType": "cq:PageContent",
"jcr:createdBy": "shiv",
"jcr:title": "samplePage",
"cq:template": "/conf/learning/settings/wcm/templates/page-content",
"jcr:created": "Sat Apr 29 2023 11:55:37 GMT+0530",
"cq:lastModified": "Sat Apr 29 2023 11:55:37 GMT+0530",
"sling:resourceType": "learning/components/page",
"cq:lastModifiedBy": "shiv",
"root": {
"jcr:primaryType": "nt:unstructured",
"layout": "responsiveGrid",
"sling:resourceType": "learning/components/container",
"container": {
"jcr:primaryType": "nt:unstructured",
"layout": "responsiveGrid",
"sling:resourceType": "learning/components/container",
"title": {
"jcr:primaryType": "nt:unstructured",
"sling:resourceType": "learning/components/title"
},
"container": {
"jcr:primaryType": "nt:unstructured",
"layout": "responsiveGrid",
"sling:resourceType": "learning/components/container",
"text": {
"jcr:primaryType": "nt:unstructured",
"text": "<p>RAM</p>\r\n",
"sling:resourceType": "learning/components/text",
"textIsRich": "true"
}
}
}
}
}
}

So in the above case, you need not explicitly pass the jcr:createdBy or jcr:created, as they are protected properties of JCR.

Hope this would help you !!!

Regards

Shiv

Shiv Prakash
Level 3
May 1, 2023

Thanks @shiv_prakash_patel 

My question was regarding more on jcr:created which I think you are mentioning as not possible. Let me know other wise.

Ideally, I could create the page using the the JSON and CURL command and like u said also am able to add the logged in user id to the jcr:createdBy. The question was inspite of jcr:createdBy or jcr:created, being the protected properties, if there was a way to enforce a different userid programatically. For Eg, a service that recreates a new page out of an existing page built by different userid and still want to maintain the existing userid to it, not the user who runs the service(Not the real usecase, but just giving an example).

Rohit_Utreja
Community Advisor
Community Advisor
May 2, 2023

Hi @sagarkmr0915 

Please have a look at the link below to achieve your use-case

https://blogs.perficient.com/2021/08/16/loading-json-content-into-aem/

 

I hope, it helps.

 

Regards,
Rohit