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

program crashing at .connect() on Android, not connecting

java android connect httpconnection

I been trying to figure this out for hours now. I've searched for answers, but I can't find an answer. I have never bothered to ask before and this is the first time I'm asking a question. Basicall...

Why does std::weak_ptr not have a move constructor or move assignment operator?

c++ visual-c++ visual-studio-2012 c++11 move-semantics

Looking through boost's 1.53 headers for weak_ptr, I was surprised to see that move assignment and move constructors were implemented even though they weren't documented. From this documentation, t...

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.