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

Doing conditional formatting on empty cells

reporting-services ssrs-2008

I'm currently building a matrix style report.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.