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. Use Groovy to invoke Liferay services the same way you would use Java. Groovy’s syntax facilitates writing concise, elegant scripts.

We’ll demonstrate by comparing Java code and Groovy code that uses UserLocalServiceUtil to retrieve a list of users and print their names to Liferay’s log.

Java

You can execute this Java code in a module deployed to DXP or you can execute the code in the Script Console:

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());
}

...

Groovy

Or you could use Groovy code in the Script Console:

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())
}

Importing com.liferay.portal.kernel.model.User and java.util.List isn’t necessary because the Script Console makes them available. And the Groovy syntax is simpler than the Java syntax.

note

If the service doesn’t have a *ServiceUtil class, use a Service Tracker to access the service.

Next Steps

Capabilities

Product

Contact Us

Connect

Powered by Liferay
© 2024 Liferay Inc. All Rights Reserved • Privacy Policy