Create Datalist through code in Alfresco

Since last few articles we have been discussing about the datalist. These are the topics which we have covered. Introduction of datalist in Alfresco, Enable datalist in Alfresco, Create custom datalist in Alfresco.

Here I am going to show you how we could create the datalist through code.We will see how to create datalist through the code.This is very useful whenever you want to create datalist in the back-end. When you have lots of items to create in datalist and you do not want to spent time by creating one by one manually.

Create Datalist through Java Code

Create datalist

Map<QName, Serializable> properties = new HashMap<QName, Serializable>();

properties.put( ContentModel.PROP_NAME,”Train Details” );

properties.put( ContentModel.PROP_TITLE, PearsonConstants.MATHSTRAND_DATALIST_NAME );

properties.put( PearsonModel.PROP_LANGUAGE, PearsonConstants.LANGUAGE_ENGLISH );

properties.put( DataListModel.PROP_DATALIST_ITEM_TYPE, "custom:trainListItem" );

NodeRef newDatalistNodeRef = nodeService.createNode( dataListNodeRef, ContentModel.ASSOC_CONTAINS, QName.createQName( "cm:myDataList"), DataListModel.TYPE_DATALIST, properties ).getChildRef();

*trainListItem which I have used here is from the previous article so all related model entries and user interface related details you could see in that post.

Now above code will create datalist container for us which could hold the datalist Items of type trainListItem type. NodeRef which is returned in above code we will be using below to create datalist Items within it.

Create datalist Items

nodeService.createNode( dataListNodeRef, ContentModel.ASSOC_CONTAINS, QName.createQName( "cm:test1" ), Qname.createQName(“custom:trainListItem”), properties );

We are using createNode API of nodeservice to create datalist container and datalist Item. above line will return us the nodeRef of newely created datalistItem.

Here first we have created simple container for datalist and then we have added one of the item into data list. You need to make sure that while creating datalist you pass right datalist Item type and same datalist Item type you need to use while creating item within datalist. Because based on the datalist Item type you specify while creating datalist Container it will identify the type of items it has to accommodate.

Also on user interface it will show the data-grid based on that datalist item Type only.

You could also use the javascript or groovy to create datalist infect above code can easily converted into javascript code.

Looking for quality Alfresco Web Hosting? Look no further than Arvixe Web Hosting!


Tags: , , , , | Posted under Alfresco | RSS 2.0

Author Spotlight


I love opensource technologies working with those technologies from the time I have stepped in to the Software Industry. Alfresco CMS is my area of expertise. I have worked on various complex implementations which involved integration of Alfresco with other technologies, extensively worked with JBPM workflows and Webscripts.

5 Comments on Create Datalist through code in Alfresco

  1. Rachana says:

    Hi mitpatoliya,
    I gone through most of the blogs you wrote. I have urgent requirement of complete custom form in share and form data will store in external database.How can i do this. Please show me path to achieve this.
    I am very thankful to you.

    • mitpatoliya says:

      Hi Rachana,
      You need to call repository side webscript on your custom form submission. That is something you could do easily in your client side javascript.
      Collect all data from your form in json object and pass it to repository webscript (POST webscript) Refer this post for calling webscript
      Then once you have all data in repo webscript which could be a java based webscript you could easily store it in any database or anywhere else.

  2. hachem says:

    Hi mitpatoliya,
    I want to create datalists that get items from an external database, such as datalist of Products, Customers, Cities, …. I am using Alfresco Community 5.0
    Thank you

    • mitpatoliya says:

      You need to create webscript which will do this kind of import for you. Before that you need to create content model where you need accomodate all tables (with each rows) from your database. Need to create associations to manage relationship between items.
      May be you could also use Bulk Import tool of Alfresco to achieve this.

      • hachem says:

        Hi mitpatoliya,
        Please how can I get access to my external database from the javascript controller of my webscript.

Leave a Reply

Your email address will not be published. Required fields are marked *