Invoking Liferay Services From Scripts

Many scripting scenarios require invoking Liferay services. Liferay *ServiceUtil classes are the fastest and most convenient way to invoke Liferay services in the script console. You can use Groovy to invoke Liferay services the same way you would use Java. Groovy’s syntax facilitates writing concise, elegant scripts.

This first example illustrates correct syntax for interacting with Liferay services. It uses UserLocalServiceUtil to retrieve a list of users and print their names to Liferay’s log. To do this, you could deploy a module with Java code like this:

import com.liferay.portal.kernel.model.User;
import com.liferay.portal.kernel.service.UserLocalServiceUtil;
import java.util.List;

...

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

for (User user:users) {
    System.out.println("User Name: " + user.getFullName());
}

...

Or you could use Groovy—based on Java—and do the whole thing right from the script console with the same code:

import com.liferay.portal.kernel.model.User;
import com.liferay.portal.kernel.service.UserLocalServiceUtil;
import java.util.List;

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

for (User user:users) {
    System.out.println("User Name: " + user.getFullName());
}

You can even make the code somewhat Groovier:

import com.liferay.portal.kernel.service.UserLocalServiceUtil

userCount = UserLocalServiceUtil.getUsersCount()
users = UserLocalServiceUtil.getUsers(0, userCount)
for (user in users){
    System.out.println("User Name: " + user.getFullName())
}

Groovy scripts that invoke Liferay services are easy to write and execute in the script console.

How to run scripts is next.

Running Scripts From the Script Console

Leveraging the Script Engine in Workflow

Script Examples

« Using Liferay's Script EngineRunning Scripts From the Script Console »
Was this article helpful?
0 out of 0 found this helpful