Search code examples
liferayhookliferay-6liferay-themeliferay-aui

How to get users list from userGroup in liferay programatically?


I am able to create a UserGroup in my custom portletm but I am unable to get the associated users list with that UserGroup. I tried in the following, which is giving an empty list as the result. Any suggestions that how can I get the list of users from UserGroup?

public void assign(ActionRequest request, ActionResponse response) 
        throws com.liferay.portal.kernel.exception.PortalException, com.liferay.portal.kernel.exception.SystemException {
    String sel_userGroupID = ParamUtil.getString(request, "selectedId");
    System.out.println("<<<< Controller assign() method userGroupID >>>>>>>>>" +sel_userGroupID);
    long usergroupid = Long.valueOf(sel_userGroupID);
    int end = UserGroupLocalServiceUtil.getUserUserGroupsCount(usergroupid);
    System.out.println("<<<< Controller assign() method total users count >>>>>>>>>" +end);
    List<UserGroup> userGroupList = UserGroupLocalServiceUtil.getUserUserGroups(usergroupid, 0 , end);
    System.out.println("<<<< Controller assign() method !! users list >>>>>>>>>" +userGroupList);
}

Solution

  • The method you are calling will return userGroups based on the given userId. If you want to get users of certain group, you need to call User API:

    Try following one:

    List<User> userList = 
          UserLocalServiceUtil.getUserGroupUsers(long userGroupId, int start, int end);
    

    There are other methods as well for userGroupId parameter, you can look at UserLocalServiceUtil for other methods.