Saturday, November 28, 2015

Custom Field/Attribute or Expando in Liferay Programmatically


We already know how to create custom field using control panel . 
Lets consider a scenario we create 40 custom fields in our local server using control panel . When i deployed my application to production server we need to create all custom fields again in production server . What if as soon as our application start and all custom fields create automatically????


Today we will create same custom field married in User entity which contain boolean value Programmatically . For this we use Event hook in Liferay. So before reading this blog it is highly recommended to read my previous blogs on Custom Fields  and  Event hook in liferay.

So lets start step by step:-

Step 1:-Create Liferay project
File-->New-->Liferay Plugin Project-->Provide name-->Select hook in plugin type-->Finish.




Step 2:-Create  hook inside project
Right click on project-->New-->Liferay hook Configuration-->tick portal.properties-->Next.




then a pop up is open.Add a new event application.startup.events and add a Class which is called on application start.




Step 3:-Check Generated Code
By using eclipse ide portal.properties file automatically created as:-

portal.properties
and liferay-hook.xml as:-

liferay-hook.xml

Step 4:-Provide Code for Custom field Creation
Open CustomFieldsCreations.java from package com.test and paste this code:-

CustomFieldsCreations.java


Explanation:-

  • run method is called when ever application starts.
  • addExpandoTable method check if table already exists than do nothing otherwise create a new table with name  CUSTOM_FIELDS.
  • addExpandoColumn method check if table CUSTOM_FIELDS contain a column with name married if yes do nothing otherwise create new column .
  • addCoumn method of ExpandoColumnLocalServiceUtil take 4 parameters tableId, Column name, type of field here we use boolean and default value which i take as unmarried ie false.

Step 5:-Check Output
Open database and check tables expandotable and expandocolumn.
Or you can go to control panel and check a new custom field is added in User entity.

Project Structure


No comments:

Post a Comment

Total Pageviews

Like Us on Facebook

Number Of Unique Visitor

Free counters!