Recent Bloggers

Jamie Sammons

10 Publications
23 octobre 2017

Jan Verweij

2 Publications
23 octobre 2017

Yasuyuki Takeo

2 Publications
18 octobre 2017

David H Nebinger

63 Publications
14 octobre 2017

Anderson Perrelli

1 Publications
9 octobre 2017

Kyle Joseph Stiemann

11 Publications
6 octobre 2017

Petteri Karttunen

2 Publications
5 octobre 2017

James Hinkey

8 Publications
5 octobre 2017

Sushil Patidar

10 Publications
4 octobre 2017

Bryan Cheung

37 Publications
4 octobre 2017
« Retour

Working with Admin Script Console

Company Blogs 11 novembre 2010 Par Jeffrey Handa Staff

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. 

First, a little background if you're not familiar with the script console or the scripting service. This feature was added in Liferay version 6.0. It was originally developed to support the execution of scripts from within the workflow module but it also provides a generic way to perform tasks that have no specific UI. A script console was also added tot he Server Administration portlet in the Control Panel to allow system administrators an easier way to execute scripts. The script engine supports the Beanshell, Javascript, Groovy, Python, and Ruby.


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. 
Réponses Auteur Données
Ya know, I bet this would be highly useful... James Falkner 11 novembre 2010 20:27
excellent! James Min 11 novembre 2010 22:37
Actually, the scripting service was implemented... Michael C. Han 12 novembre 2010 10:14
Yeah, I know it's been there, but during... James Falkner 12 novembre 2010 10:35
Got it. Yeah, the scripting ide is something... Michael C. Han 12 novembre 2010 13:59
Thank you, @Michael, @JF and @James. Both... Jonas Yuan 15 novembre 2010 08:09
Thanks for the clarification Mike! Jeffrey Handa 12 novembre 2010 10:42
I've been able to use the script console to add... Kevin Britton 29 novembre 2010 12:26
This is a great tool, but here are two things I... Joseph Toman 3 juin 2011 22:10
Is there any way to "cron" that scripts? I... Moisés Belda 23 juin 2011 04:24
[...] Many of you are probably aware that... Anonyme 1 novembre 2011 16:40
[...] Since the release 6.0, Liferay provide a... Anonyme 12 mai 2013 23:34
You can get some extra tips about Admin Script... Sébastien Le Marchand 23 mai 2013 13:37
Looking all over to find the answer and I see... Wendell Payne 31 juillet 2017 08:22
Hi Wendell, I'm not aware of any way to... Jeffrey Handa 7 août 2017 14:59

Ya know, I bet this would be highly useful during WCM template development. A nice prototyping feature. Now, to make this available via the OS command line emoticon
Publié le 11/11/10 20:27.
Publié le 11/11/10 22:37.
Actually, the scripting service was implemented prior to Kaleo. It was for the EU research project, Romulous. The service's primary responsibility was to make it easier for developers to write portlets in non-Java languages (e.g. Ruby, Groovy, Python, PHP). As a side benefit, workflow was able to use it for easier action coding and of course we added it as an admin interface.

@JF scripting in WCM template was already possible with the velocity and freemarker support. There's no need for scripting service for that. In the WCM template, you can call any LR service. e.g.:
Publié le 12/11/10 10:14.
Yeah, I know it's been there, but during template development you have 1. have a structure defined, 2. define your template, 3. create an article, 4. deploy to a WCM display portlet, to see if it works. It would be awesome if during template development I could have a separate window open to run small snippets of test script in VM or FM or whatever, to make sure it works, before committing it to the template. Even cooler would be a "test script" button which just runs what you have written with some sample data or something. Really would speed up template dev!
Publié le 12/11/10 10:35 en réponse à Michael C. Han.
Thanks for the clarification Mike!
Publié le 12/11/10 10:42 en réponse à Michael C. Han.
Got it. Yeah, the scripting ide is something we need. something we are looking @ for the workflow designer as well.
Publié le 12/11/10 13:59 en réponse à James Falkner.
Thank you, @Michael, @JF and @James. Both scripting and WCM templates are cool features. Comparing scripting and WCM templates (VM or FM), which is better for developers?

Maybe, detailed documentation would be helpful to show these cool features.
Publié le 15/11/10 08:09 en réponse à James Falkner.
I've been able to use the script console to add a user to a community, but it seems to fail whenever I try to do an addUser. Is this a designed-in limitation, or am I doing something wrong?
Publié le 29/11/10 12:26.
This is a great tool, but here are two things I can think of that would make this even more useful:

a) Run scripts from the Document Library, or some other persistence mechanism. If you have to cut & paste every time you want to run a script, that gets old real fast.
b) Redirect output to a popup window. The admin does not necessarily have access to the logs or console when they are running a script.
Publié le 03/06/11 22:10.
Is there any way to "cron" that scripts?
I need to execute one script periodically, with the easy API access that provide this scripting method.... it's possible?
Publié le 23/06/11 04:24.
[...] Many of you are probably aware that Liferay version 6.0 supports several scripting languages in a variety of contexts including portlet plugins, Kaleo workflows, and the Script Console in the Server... [...] Read More
Publié le 01/11/11 16:40.
[...] Since the release 6.0, Liferay provide a script console integrated into the Control Panel. Very powerful, this tool becomes fast essential for any portal administrator or developer (if you do not... [...] Read More
Publié le 12/05/13 23:34.
You can get some extra tips about Admin Script Console here :
Publié le 23/05/13 13:37.
Looking all over to find the answer and I see all combinations but not a direct explanation Is there a capability to use an API or CLI to communicate and execute commands to the LifeRay console?
Publié le 31/07/17 08:22.
Hi Wendell, I'm not aware of any way to interact with the Script Console from a CLI, however you could interact with Liferay's web services.

You might try posting your question to the Forums (be sure to specify the Liferay version you're using and any specific commands you're trying to execute).

If you're new to the Liferay community, Jamie's recent blog post (­h-the-liferay-community) does a good job outlining other resources for getting help including our Community Slack Channel.
Publié le 07/08/17 14:59 en réponse à Wendell Payne.