Expand my Community achievements bar.

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

Avatar

Level 3

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

7 Replies

Avatar

Community Advisor

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

Avatar

Level 3

@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?

Avatar

Community Advisor

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

Avatar

Community Advisor

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

Avatar

Level 3

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

Avatar

Community Advisor

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