liferayliferay-6liferay-auiliferay-ide

Show all users liferay


I need to make a portlet which would show all registred users in liferay.

Im not asking you to write all code here, but I would aprreciate if u could present an step-by-step plan of actions, cause I really don't understand how to get info from database.

UPD: 1. I can`t solve what should I import in java file. import java.io.IOException; import java.util.List;

import javax.portlet.PortletException;
import javax.portlet.PortletPreferences;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.transaction.SystemException;

import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.model.User;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
public class ShowUsers extends MVCPortlet {

public void render(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException{
            Log log = LogFactoryUtil.getLog(ShowUsers.class);
            List<User> users = null;
            try {
                users = UserLocalServiceUtil.getUsers(QueryUtil.ALL_POS, QueryUtil.ALL_POS);
            } catch (com.liferay.portal.kernel.exception.SystemException e) {
                log.info("Exception happened");
            }
            renderRequest.setAttribute("allUsers", users );
            super.render(renderRequest, renderResponse);

     }

}

=================== My jsp file:

    <%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
    <%@ page import="java.util.List" %>
    <%@ page import="com.liferay.portal.model.User" %>
    <%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> 
    <portlet:defineObjects /> 
    <%
         List<User> thatusers = renderRequest.getAttribute("allUsers");
    %>

    <ul>
    <% for (User user : thatusers) { %>
    <li><%= user %></li>
    <% } %> 
    </ul>

And after this code I have strange info about all users and I need only it's name! this:

{uuid=fb7224c0-2488-45c1-97b8-5608450435a6, userId=20199, companyId=20155, createDate=2016-06-06 08:14:14.0, modifiedDate=2016-06-06 08:14:14.0, defaultUser=false, contactId=20200,


Solution

  • To get all users take a look at UserLocalServiceUtil.getUsers()

    In your portlet class you need to pass this list to the jsp you are serving:

    public void doView(RenderRequest renderRequest, RenderResponse renderResponse)
    throws IOException, PortletException {
        List<User> users =  UserLocalServiceUtil.getUsers(QueryUtil.ALL_POS, QueryUtil.ALL_POS);
        renderRequest.setAttribute("allUsers", users );
        super.doView(renderRequest, renderResponse);
    }  
    

    And than in your jsp iterate the allUsers list using JSTL to get a user object.

    getUsers(QueryUtil.ALL_POS, QueryUtil.ALL_POS); would retrieve all the users, instead of QueryUtil.ALL_POS you can specifie start and end if you need to paginate the result.