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"%>
6 comments:
Hi Atin,
It doen't work for me. The codes show me 2 error2 :
1. Error : company cannot be resolved. in this code
List list = UserLocalServiceUtil.search(
company.getCompanyId(), keywords, true, userParams,
searchContainer.getStart(), searchContainer.getEnd(),
OrderByComparator) null);
2. Error : company cannot be resolved. in this code:
total = UserLocalServiceUtil.searchCount(
company.getCompanyId(), keywords, true, userParams);
pageContext.setAttribute("results", list);
pageContext.setAttribute("total", total);
Please help me out
Henky Rumbo
henky_rumbo@yahoo.com
Hi SUN,
You can use themeDisplay.getCompanyId() in place of company.getCompanyId().
Gracias amigoo me sirvo mucho ese código.
how can I get searchContainer
I am using Liferay Search portlet In which I want to get a Id of search button to trigger a autoclick event.
Can you please help me for that.
Nice post! But I don't understand how do the pagination. Because I create the url, put iterator="<%=url%>, and put , but the pagination doesn't work, it shows the buttons but don't work. I have to implement any specific function in the domain to return the list? or if I get a list with all the elements its correct?
I don't have idea because it doesn't work.
Please, can yoy help me?
Thank you very much.
Post a Comment