To create content data from script

Hi,

I have created page component(hello-region) and part component(person-intro) cretaed in my app. Is it possible to create the content data(person type) with create api like below?

The requirement is to create content, from json data exported from another cms system and import it to Enonic.

var page_result =contentLib.create({
name: ‘person 1’,
parentPath: ‘/Test’,
displayName: ‘Person 1’,
contentType: app.name + ‘:person’,
branch: ‘draft’,
language: ‘en’,
data: {
“pname”: “some name”,
“email”: “some email”
},
page: {
controller: app.name +":hello-region",
config: {},
regions: {
main: {
components: [
{
name: “Person Details”,
path: “main/0”,
type: “part”,
descriptor: app.name +":person-intro",
config: {
introduction: ‘some introduction’,
role: “some role”
}
}
],
name: “main”
}
}
},
attachments: {},
publish: {}
});

Hi, Vanditha!

The create() function in the content library is the basic and safe way to create content, with the drawback that you cannot add more advanced data, such as what you have in the “page: {…}” property that you provided above. In order to add page data, you must use the node library. Personally, I would first create the content using the content library to ensure safe creation of required data, then I would modify the content using the node library.

Add the node library to your project. After having created the content using your contentLib.create(), connect to the draft branch using the nodeLib.connect() and then with the object you get in return, use that object’s modify() function sending in the ID of the content and an editor function that adds the extra page property with the desired page data.

Documentation for modify() in a node library RepoConnection: http://repo.enonic.com/public/com/enonic/xp/docs/6.15.5/docs-6.15.5-libdoc.zip!/module-node-RepoConnection.html#modify

1 Like

Thanks you @bhj. It is working.