Today we will discuss about Custom Field in Liferay.Lets consider a scenario in which you want that User entity must have one extra field like married. How will you add a new attribute to existing Liferay entity User.Custom fields in liferay is the solution for this problem.
Custom fields are designed to allow end users to extends the default data entities like User, Group etc.You can create custom field programmatically as well as Using Control Panel. Today we create Custom Field in User entity by Control Panel.
Expando provides mechanism to create tables, columns, rows and values dynamically.Four tables contain entries for custom fields:-
a)expandotable
b)expandorow
c)expandocolumn
d)expandovalue
So lets start step by step:-
Step 1:-Create Custom Field
Go to Control panel-->Configuration-->Custom Fields. A pop up window is open as:-
Click on edit. A new window is open now click on Add Custom Field button as:-
Then a new window is open as:-
Provide key as married and type as true/false and press save.
Then press Action-->Edit .here you can edit the property of your custom field as:-
Note:-
- The key of custom field is used to access the attribute programmatically through the <liferay-ui:custom-attribute> and <liferay-ui:custom-attribute-list />.
- The key must not contain spaces or spacial character.
Step 2:-Check tables entries in database
Login in your data base and check entries that are created when you create custom fields .Two tables that are changed are:-
a)expandotable
b)expandocolumn
Note:- Only two tables are changed but expandorow and expandovalue are remain same.
Go to Control panel-->Users and Organisation-->Add-->User
provide all details and save.
Then Go to Miscellaneous-->Custom Field-->Select True/false click save.
Step 4:-Check tables entries in database
Login in your data base and check entries that are created when you create create User and add Custom fields to that User .Two tables that are changed are:-
a)expandorow
b)expandovalue
Note:- ClassPk in expandorow and expandovalue is the UserId of User_ table.
Step 5:-Get the Custom Field in Program
Create a portlet and paste this code inside doView() as:-
Now login as the User created in Step 3 and deploy the portlet and see the output.
Second Approach for getting the value
Login as Admin and change the permission of your custom fields as:-
Create a portlet and paste this code inside doView() as:-
Now login as the User created in Step 3 and deploy the portlet and see the output.
Thats it Hope this will Help.....
Related Post:-