First of all, greetings to everyone in the Liferay Community. I've been lucky enough to meet many of you around the world over the last two years and I look forward to meeting more of you in the years to come. As they say, time really does fly when you're having fun. I can't belive it's taken me this long to writing my first post, and I hope it doesn't take another two years to write my next :)
Today I wanted to share a little bit of information about the new Liferay Administrator Script Console that's available through the Liferay Control Panel. I know that many of you have probably been wondering about this feature and perhaps thought that it wasn't working because the default script doesn't work as-is. Well last week I finally had a chance to spend a little bit of time playing around with it and I wanted to share what I learned with the community.
Some of the tasks that you might use the script console to perform might include user maintenance operations, bulk manipulations using the Liferay API to ensure consistency, or even system level operations.
To see a very simple example of the script console in action, navigate to the Control Panel → Server Administration → Script. Chance the script type to Groovy and modify the current code to look like the following:
number = com.liferay.portal.service.UserLocalServiceUtil.getUsersCount();
Click the execute button and check the console or the log for your output.
Let's start with a slightly more complex example to retrieve some user information from the database. To change things up a bit, let's start by setting the script type to Python. There are some known issues with Liferay CE (6.0.5) and Liferay EE(6.0.10) that causes the script type to reset after every execution so be sure to double check the script type to make sure it's set correctly. With the script type set to Python, execute the following code and watch the console:
from com.liferay.portal.service import UserLocalServiceUtil
maxNumber = UserLocalServiceUtil().getUsersCount()
from com.liferay.portal.model import User
users = UserLocalServiceUtil().getUsers(0,maxNumber)
for user in users:
print user.getFullName(), " -- ",user.getLastLoginDate()
That's all that's needed to run scripts and to access the Liferay service layouer. However, there are some things to keep in mind when working with the script console:
- There is no undo
- There is no preview
- There is no permission checking
- Scripts are executed synchronously, so be careful with scripts that might take a long time to execute.