This method will search the users on the basis of keywords entered.
These keywords (space separated) may occur in the user's first name, middle name, last name, screen name, or email address .
First of all you have to create a simple input text field in your jsp.
Also, create a simple renderURL to navigate to the search-users jsp with the entered keywords.
In some jsp e.g. view.jsp :
<portlet:renderURL var="searchURL">
<portlet:param name="jspPage" value="/html/search-users.jsp" />
</portlet:renderURL>
<aui:form name="searchForm" action="<%=searchURL%>" method="post">
<aui:input name="keywords" label="Enter any keyword related to user you wish to search"/>
<aui:button type="submit" value="Search" />
</aui:form>
In search-users.jsp :
Here we will be using search container to show the results of the searched user(if found).
Results of the search container would be the list we get from search method of UserLocalServiceUtil which also include our parameter "keywords" which we will be getting from the request.
Total would be the total count of the list.
<%
String keywords = ParamUtil.getString(request, "keywords");
PortletURL renderURL = renderResponse.createRenderURL();
renderURL.setParameter("jspPage", "/html/search-users.jsp");
// here we have created renderURL just to remain on the current jsp page after the pagination.
%>
<liferay-ui:search-container iteratorURL="<%= renderURL %>" emptyResultsMessage="there-are-no-records" delta="5">
<liferay-ui:search-container-results>
<%
LinkedHashMap<String, Object> userParams = new LinkedHashMap<String, Object>();
List<User> list = UserLocalServiceUtil.search(
company.getCompanyId(), keywords, true, userParams,
searchContainer.getStart(), searchContainer.getEnd(),
(OrderByComparator) null);
total = UserLocalServiceUtil.searchCount(
company.getCompanyId(), keywords, true, userParams);
pageContext.setAttribute("results", list);
pageContext.setAttribute("total", total);
%>
</liferay-ui:search-container-results>
<liferay-ui:search-container-row className="com.liferay.portal.model.User" modelVar="record">
<liferay-ui:search-container-column-text name="User Id" property="userId" />
<liferay-ui:search-container-column-text name="First Name" property="firstName" />
<liferay-ui:search-container-column-text name="LastName" property="lastName" />
// any other information you want to display
</liferay-ui:search-container-row>
<liferay-ui:search-iterator />
</liferay-ui:search-container>
Note: Please do import the required classes at the top of your jsp page.
like :
<%@ page import="com.liferay.portal.kernel.util.ParamUtil"%>
<%@ page import="javax.portlet.PortletURL"%>
<%@ page import="com.liferay.portal.kernel.util.OrderByComparator"%>
<%@ page import="java.util.List"%>
<%@ page import="java.util.LinkedHashMap"%>
<%@ page import="com.liferay.portal.model.User"%>
<%@ page import="com.liferay.portal.service.UserLocalServiceUtil"%>
These keywords (space separated) may occur in the user's first name, middle name, last name, screen name, or email address .
First of all you have to create a simple input text field in your jsp.
Also, create a simple renderURL to navigate to the search-users jsp with the entered keywords.
In some jsp e.g. view.jsp :
<portlet:renderURL var="searchURL">
<portlet:param name="jspPage" value="/html/search-users.jsp" />
</portlet:renderURL>
<aui:form name="searchForm" action="<%=searchURL%>" method="post">
<aui:input name="keywords" label="Enter any keyword related to user you wish to search"/>
<aui:button type="submit" value="Search" />
</aui:form>
In search-users.jsp :
Here we will be using search container to show the results of the searched user(if found).
Results of the search container would be the list we get from search method of UserLocalServiceUtil which also include our parameter "keywords" which we will be getting from the request.
Total would be the total count of the list.
<%
String keywords = ParamUtil.getString(request, "keywords");
PortletURL renderURL = renderResponse.createRenderURL();
renderURL.setParameter("jspPage", "/html/search-users.jsp");
// here we have created renderURL just to remain on the current jsp page after the pagination.
%>
<liferay-ui:search-container iteratorURL="<%= renderURL %>" emptyResultsMessage="there-are-no-records" delta="5">
<liferay-ui:search-container-results>
<%
LinkedHashMap<String, Object> userParams = new LinkedHashMap<String, Object>();
List<User> list = UserLocalServiceUtil.search(
company.getCompanyId(), keywords, true, userParams,
searchContainer.getStart(), searchContainer.getEnd(),
(OrderByComparator) null);
total = UserLocalServiceUtil.searchCount(
company.getCompanyId(), keywords, true, userParams);
pageContext.setAttribute("results", list);
pageContext.setAttribute("total", total);
%>
</liferay-ui:search-container-results>
<liferay-ui:search-container-row className="com.liferay.portal.model.User" modelVar="record">
<liferay-ui:search-container-column-text name="User Id" property="userId" />
<liferay-ui:search-container-column-text name="First Name" property="firstName" />
<liferay-ui:search-container-column-text name="LastName" property="lastName" />
// any other information you want to display
</liferay-ui:search-container-row>
<liferay-ui:search-iterator />
</liferay-ui:search-container>
Note: Please do import the required classes at the top of your jsp page.
like :
<%@ page import="com.liferay.portal.kernel.util.ParamUtil"%>
<%@ page import="javax.portlet.PortletURL"%>
<%@ page import="com.liferay.portal.kernel.util.OrderByComparator"%>
<%@ page import="java.util.List"%>
<%@ page import="java.util.LinkedHashMap"%>
<%@ page import="com.liferay.portal.model.User"%>
<%@ page import="com.liferay.portal.service.UserLocalServiceUtil"%>