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