Thursday, 10 October 2013

How to get Custom Field value in Liferay using liferay API?

This code is done for the Expando of User Model (UserExpando) and the custom filed "userCity"



//////////////////////////////

LiferayFacesContext liferayFacesContext = LiferayFacesContext.getInstance();
               
                long companyId = liferayFacesContext.getCompanyId();
               
                User user = liferayFacesContext.getUser();
                System.out.println("User info "+user.getUserId()+" --- "+user.getFirstName()+" ---- "+user.getEmailAddress());
                Locale locale = liferayFacesContext.getLocale();
                ExpandoValue val=null;
                ExpandoTable expandoTable = ExpandoTableLocalServiceUtil.getDefaultTable(companyId,User.class.getName());
                long expandoTableId=expandoTable.getTableId();
                ExpandoColumn column = ExpandoColumnLocalServiceUtil.getColumn(expandoTableId, UserExpando.USER_CITY.getName());
                 if (column != null) {
                 val = ExpandoValueLocalServiceUtil.getValue(expandoTableId, column.getColumnId(), user.getUserId());
                   if (val != null) {
                     System.out.println("val====================="+ val);
                   }
                  
                }

/////////////////////////////////

How to get logged In User detail in liferay?

How to get logged In User detail in liferay?

These lines of code will give you the user who is logged in from the LiferayFaces Context.

LiferayFacesContext liferayFacesContext = LiferayFacesContext.getInstance();
                
 long companyId = liferayFacesContext.getCompanyId();
                
 User user = liferayFacesContext.getUser();                       //User is an Model of liferay