6

I added a custom user field in Liferay, and set a value on a specific user.

How can I access this value programmatically?

If I try this, I always get null:

String customAttr = (String)user.getExpandoBridge().getAttribute("customAttr");

user.getExpandoBridge().getAttribute("customAttr") returns a value of Type java.IO.Serializable.

Maybe the cast here is wrong?

But the Custom Attribute does exist (following code prints out the attribute key):

for (Enumeration<String> attrs = user.getExpandoBridge().getAttributeNames(); attrs.hasMoreElements();)
    _log.info("elem: '" + attrs.nextElement() + "'");

Somehow I miss the point here....

Daniel Kreiseder
  • 11,397
  • 8
  • 35
  • 57

3 Answers3

8

It was a security problem...

In com.liferay.portlet.expando.service.impl.ExpandoValueServiceImpl.getData(String className, String tableName, String columnName, long classPK):

    if (ExpandoColumnPermission.contains(
            getPermissionChecker(), column, ActionKeys.VIEW)) {

        return expandoValueLocalService.getData(
            className, tableName, columnName, classPK);
    }
    else {
        return null;
    }

I only had to set the view permisson on the custom expando value, and everything worked fine.

Daniel Kreiseder
  • 11,397
  • 8
  • 35
  • 57
3

I know it's a bit late, but for those still trying to figure out why a custom field turns out to be null (although it is clearly set and visible in Liferay), please make sure first that the custom field has the permissions properly set (Control Panel -> Custom fields -> User -> choose the appropiate custom field and click Action -> Permissions). By default, the Owner has all rights, but in my case, for example, I needed a View permission with a Guest account (user in the process of logging in). Hope this helps.

Andra
  • 31
  • 1
  • yes, giving permission guest would work for me as I tried to access custom fields(expando) over site leve(group) and getting data by hitting rest service without login. please make guest permission view – asifaftab87 Apr 13 '21 at 08:23
0

Check here how to fix the issue when custom fields (expando fields) are exported for users in CSV http://liferay.bdedov.eu/2012/02/exporting-user-custom-fields-in-csv.html. If you want to make an export of users and define custom fields to be included in the export then you receive only null values for for the custom fields. Check out this post to see how to fix this.