Liferay Custom Field or Custom Attributes

Aim of this Article:

Understanding the Liferay Custom Field concept.I will explain how to add custom field through Liferay Control Panel.

Use of Custom Field :

Add extra field in database table through UI using Liferay API.


Note

Note:  Custom Field or Custom Attributes both are same


Liferay Custom Fields or Liferay Custom Attribute is a feature in Liferay. Liferay provides UI and API to allow for the creation of customized fields associated with any entity generated by Service Builder. These fields provide extensibility without the need for extending the Liferay DB schema.

Before going to the topic lets have a look in Liferay Entity. If you have used Liferay Service Builder then you are aware of Liferay Entity. An entity is simply a Java Class which is auto generated by Service Builder. An Entity represents model data and persisted in database. Liferay by default it has many entities. Some of them are User,Page,Role and many more.

 

Let’s explore how to add custom fields through UI. You need to login to your portal as Admin. Go to your control panel. Click Custom Fields as shown in red color in below picture.

Control Panel

You will see below list

 

Custom Fields

You will see list of all entities  for which you can add custom fields. However you can add your own Entity in this list. I will cover how to add your own entity in another session. 

Now you can add your custom fields for any entity in the list. Consider you want to add custom fields for User entity. Click edit for the entity User. 



Follow the below screen shot

secondary-emailSelect type as Text Field. There are many other options under type. You can explore yourself. You can add many custom fields for a given Entity. For this session we will concentrate on only one field. 

After adding the custom field we can view, update from the Liferay UI as well as we can use in our custom portlet.  

View and Update Custom Field By Code: 
In your jsp add below code 

    <%@page import="com.liferay.portal.model.User"%>

    <%
      User currentUser = themeDisplay.getUser();

    %>
    <portlet:actionURL var="updateCustomField" name="updateUserCustomeField"/>

    <aui:form action="<%= updateCustomField %>" method="post" name="fm">

    <liferay-ui:custom-attribute-list
    className="<%= User.class.getName() %>"
    classPK="<%= currentUser != null ? currentUser.getUserId() : 0 %>"
    editable="<%= true %>" label="true"/>
    <aui:button type="submit" value="update"/>
    </aui:form>
 

 


In your portlet class file add below code 

 @ProcessAction(name="updateUserCustomeField")
 public void updateUserCustomeField(ActionRequest actionRequest,
 ActionResponse actionResponse) throws IOException,
 PortletException, PortalException, SystemException {

ServiceContext serviceContext = ServiceContextFactory.getInstance(User.class.getName(), actionRequest);

User user = PortalUtil.getUser(actionRequest);

user.setExpandoBridgeAttributes(serviceContext);

UserLocalServiceUtil.updateUser(user);
  }
 

 


Show all the custom fields for a given Entity(in JSP): 

 


 <liferay-ui:custom-attribute-list
 className="<%= User.class.getName() %>"
 classPK="<%= currentUser != null ? currentUser.getUserId() : 0 %>"
 editable="<%= false %>" label="true"/>

 

Show specific Custom Field Value (in JSP) : 

 


<liferay-ui:custom-attribute
 className="<%= User.class.getName() %>"
 classPK="<%= currentUser != null ? currentUser.getUserId() : 0 %>"
 editable="<%= false %>"
 name="secondary-email" label="true"/> 

 

 Add Custom Field dynamically by Java Code: 

 


@ProcessAction(name="addUserCustomeField")
 public void addUserCustomeField(ActionRequest actionRequest,
 ActionResponse actionResponse) throws IOException,
 PortletException, PortalException, SystemException {

String customField = ParamUtil.getString(actionRequest, "customField","");
  User user = PortalUtil.getUser(actionRequest);
  user.getExpandoBridge().addAttribute(customField);
  }

 

To call this method add below code in your JSP page:

<portlet:actionURL  var="addCustomField" name="addUserCustomeField"/>
<aui:form action="<%= addCustomField %>" method="post" name="fm">
<aui:input name="customField" label="Custom Field"/>
<aui:button type="submit" value="Add Custom Field"/>
</aui:form>

Download Source Code

About The Author

2 thoughts on “Liferay Custom Field or Custom Attributes”

  1. Error getting standard attribute value in a Liferay HOOK

    I have a Liferay form in a HOOK in which I have two standard attributes (custom created to create a user) but when trying to get the value of the attributes (from a class that handles the struts actions for the create_account.jsp hook) I get null. At the time of trying to get the values the user is not logged in the portal because the user will be created until now. The following is my code:

    Code of create_account.jsp

    <aui:form action="” method=”post” name=”fm”>

    <liferay-ui:custom-attribute
    className="” classPK=””
    editable=”” label=”” name=”EPLND” />

    <liferay-ui:custom-attribute
    className="” classPK=””
    editable=”” label=”” name=”TDCTD” />

    Code of the class that handles the action struts

    public class EventoAccionCAStruts extends BaseStrutsPortletAction {

    public void processAction(StrutsPortletAction originalStrutsPortletAction,
    PortletConfig portletConfig, ActionRequest actionRequest,
    ActionResponse actionResponse) throws Exception {

    User user = PortalUtil.getUser(actionRequest);
    String vctdc_td_epl = user.getExpandoBridge().getAttribute(“TDCTD”); //Aqui llegan el valor nulo
    Long nmepl_nd = user.getExpandoBridge().getAttribute(“EPLND”); //Aqui llegan el valor nulo

    originalStrutsPortletAction.processAction(originalStrutsPortletAction,
    portletConfig, actionRequest, actionResponse);

    }

    }

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top