How to get email addresses of all users in liferay

I want to get email addresses of all users in liferay... I have used the following code..

DynamicQuery dynamicQuery = UserLocalServiceUtil.dynamicQuery();
dynamicQuery.add(RestrictionsFactoryUtil.isNotNull("emailAddress"));
int startOfList = 0;
int endOfList = 100;
List<User> articles = (List<User>) UserLocalServiceUtil.dynamicQuery(dynamicQuery, startOfList, endOfList);

Can somebody tell me where am I doing mistake.

I am getting:

  1. java.lang.InstantiationException and
  2. com.liferay.portal.kernel.deploy.hot.HotDeployException: Error registering portlets for ControlPanelPortlet-portlet.

ControlPanelPortlet is the name of my portlet.

Answers


Try this code..

List<User> users = UserLocalServiceUtil.getUsers(0, UserLocalServiceUtil.getUsersCount());

for(User user : users) {
     System.out.println(user.getEmailAddress());
}

Let me know if you have some problem


If you want to fetch just the list of email-addresses of all the Users then you can use the following code:

DynamicQuery dynamicQuery = UserLocalServiceUtil.dynamicQuery();

dynamicQuery.add(RestrictionsFactoryUtil.isNotNull("emailAddress"));

// this is the statement which would cause to return only a list of emails
dynamicQuery.setProjection(ProjectionFactoryUtil.property("emailAddress"));

int startOfList = QueryUtil.ALL_POS;

int endOfList = QueryUtil.ALL_POS; // this will fetch all the Users

// note: it returns a list of String
List<String> emailAddresses = (List<String>) UserLocalServiceUtil.dynamicQuery(dynamicQuery, startOfList, endOfList);

If you want to fetch the Users then you code is fine, just need to change the startOfList and endOfList field:

DynamicQuery dynamicQuery = UserLocalServiceUtil.dynamicQuery();

dynamicQuery.add(RestrictionsFactoryUtil.isNotNull("emailAddress"));

int startOfList = QueryUtil.ALL_POS;

int endOfList = QueryUtil.ALL_POS;

List<User> users = (List<User>) UserLocalServiceUtil.dynamicQuery(dynamicQuery, startOfList, endOfList);

or if you don't want to check for any condition and want to fetch the Users then just use:

List<User> users = UserLocalServiceUtil.getUsers(startOfList, endOfList);

And regarding the exceptions you are seeing, they are not related to the piece of code you have posted. These may due to some other code either in Java class or JSP. If you put the full stack-trace then may be we can help.


Need Your Help

What is the correct std::set_union code?

c++ algorithm stl set-union

This site claims that set_union is equivalent to the following code:

Writing variables to a session file manually with php

php file session fopen

I have an unusual problem. I need to be able to alter a session variable manually for any given user's session (other than my own). The application manages employees in the call center.