Wednesday 20 August 2014

Using Custom Fields/Custom Attributes in Liferay 6.1


Some time we may come across the need for adding a property or storing data associated with OOTB portlets. We dont need to create any new service. It can be done with the help of Custom Attributes or Custom Fields. Liferay provides this feature through Expando services.

The mechanism is supported by using 4 database tables: ExpandoTable, ExpandoColumn, ExpandoRow and ExpandoValue.

Expando values can be added to liferay objects like: Users, Documents and Media, Web Contents, Bookmarks, etc. through control panel and programmatically to the custom portlets as well.

For Custom-Portlets:

Create custom-fields for custom-table:
Suppose we want to create the custom fields into custom-table. e.g Let us create a custom field named
PrimaryTag and associate it with a custom portlet named VideoManagement.
Example code:

It will add an entry in all 4 expando-tables mapped with VideoManagement classname.


Display custom-fields into your webpage(for custom portlet)
Liferay provides below code to display custom fields on page for all the OOTB portlets.
Same code can be used in custom-portlet to display them as text fields.


In LocalServiceImpl add following code for storing expandovalue:


The way of setting value:


e.g addCustomAttribute(_companyId, _className, PrimaryTag", _classPK, “primaryTagValue”);
i.e. ExpandoValueLocalServiceUtil.addValue(classNameId, tableId, columnId, classPK, data);


The way of getting value:


Expando Table:

Expando Column:


Expando Value:

where tableId is the id of default table, columnId the id of retrieved column and classPK the primary key of entity.

For OOTB portlets e.g journalArticle, we have to first create custom attributes from control panel.

The following code sets attributes for particular article using service context.


Thanks for reading! Happy Learning!

1 comment: