-1

I need to get the list of User Groups for a given user. That user should can either be the current logged in user or any other user in Liferay. What is the Liferay API Call that I need to use?

Is using UserGroupLocalServiceUtil the only way to do it?

Ashok Goli
  • 5,043
  • 8
  • 38
  • 68

1 Answers1

3

There are a couple of ways which can retrieve the User Group List.

  1. Retrieve the User object first and call the getUserGroups() on the User Object as shown below

    List<UserGroup> userGroupList = user.getUserGroups();

  2. Use the UserGroupLocalServiceUtil class to retrieve the User Group List.

    List<UserGroup> userGroupList = UserGroupLocalServiceUtil.getUserUserGroups(user.getUserId());

There are many other useful methods that can be utilized.

  • To paginate the retrieved User Group List through these methods:

    List<UserGroup> userGroupList = UserGroupLocalServiceUtil.getUserUserGroups(userId, start, end);

  • To paginate and sort the User Group List

    List<UserGroup> userGroupList = UserGroupLocalServiceUtil.getUserUserGroups(userId, start, end, orderByComparator)

  • To get the number of User Groups for a User

    int userGroupCount = UserGroupLocalServiceUtil.getUserUserGroupsCount(userId);

To get the User object, you can use any of the following API calls.

User user = UserLocalServiceUtil.getUserById(userId);
User user = UserLocalServiceUtil.getUserByScreenName(companyId, screenName);
User user = UserLocalServiceUtil.getUserByEmailAddress(companyId, emailAddress);

The companyId in the above calls can be retrieved using:

long companyId = PortalUtil.getCompanyId(request);

To get the current logged in user, refer https://stackoverflow.com/a/10555521/668240

Community
  • 1
  • 1
Ashok Goli
  • 5,043
  • 8
  • 38
  • 68