Get list of users in liferay

4.3k views Asked by At

I want to display users list on liferay. and I am having problem with it. Here is my action class.

public void userList(ActionRequest actionRequest, ActionResponse actionResponse) throws SystemException {
    // Todo Logic for user code
    try {
        int countUser = UserLocalServiceUtil.getUsersCount();
        log.info("User Present In DB" + countUser);
        List < User > users = UserLocalServiceUtil.getUsers(0, countUser);
        PortletSession sessions = actionRequest.getPortletSession();
        sessions.setAttribute("users", users);
        log.info("Session set from My Portlet" + sessions.getAttribute("users"));
        for (User user: users) {
            if (user != null) {
                log.info("UserID--:" + user.getUserId() + "UserCompanyID-:" + user.getCompanyId() + "UserEmail-:" + user.getEmailAddress() +
                    "UserScreenName--:" + user.getScreenName());
            }
        }

and how I am trying to get the users list on jsp.

<%@page import="com.test.UserList.userList"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.liferay.portal.model.User"%>
<%@page import="java.util.List"%>
<%@page import="javax.portlet.PortletSession"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@taglib uri="http://alloy.liferay.com/tld/aui" prefix="aui" %>

<portlet:defineObjects />
<%
     PortletSession session2 = renderRequest.getPortletSession();
     ArrayList<User> users = (ArrayList) session2.getAttribute("users");

     if(users!=null){
%>
    <b>Name: </b><%=users.get(users) %>
<%} %>

and i am getting the value is null i want to display all the users name in list

2

There are 2 answers

0
Parkash Kumar On BEST ANSWER

There is no issue with your logic in putting user-list in session, the list is getting populated and set in session accurately. However, on JSP, there are couple of issues:

  1. renderRequest is undefined.
  2. Cast exception (UnmodifiableList cannot be cast to java.util.ArrayList) for user-list.
  3. You are not iterating list and getting user objects properly.

So, you need to do as following on your JSP:

<%@page import="java.util.ArrayList"%>
<%@page import="javax.portlet.RenderRequest"%>
<%@page import="com.liferay.portal.model.User"%>
<%@page import="javax.portlet.PortletSession"%>

<%
    RenderRequest renderRequest = 
        (RenderRequest) request.getAttribute("javax.portlet.request");
    PortletSession session = renderRequest.getPortletSession();
    List<User> users = (List<User>) session.getAttribute("users");

    if(users != null){
        for(User user : users){
%>
            <b>Name: </b><%=user.getLastName() + ", " + user.getFirstName() %><br />
        <%}
    }%> 

(Tested Code)

0
Neeraj Gautam On

Another option to get list of all users is

UserLocalServiceUtil.getUsers(QueryUtil.ALL_POS, QueryUtil.ALL_POS);

and then iterate it to fetch each user.