Objective of this article: Understand Liferay Beanshell feature.
Use of Beanshell: It allows us to execute java code directly from Liferay UI.
Beanshell is a Java scripting language that’s designed to run Java code with little or no changes. Beanshell doesn’t support the use of Java Generics.
To use beanshell go to your control panel. Access the beanshell as shown in below screen shot
Below are the implicit objects that we can use in beanshell
- portletConfig
- portletContext
- actionRequest
- actionResponse
- out
import com.liferay.portal.model.User; import com.liferay.portal.service.UserLocalServiceUtil; import java.util.List; int userCount = UserLocalServiceUtil.getUsersCount(); out.println(userCount);
import com.liferay.portal.util.PortalUtil; long companyId = PortalUtil.getCompanyId(actionRequest ); out.println("The comapny Id is : "+companyId);
import com.liferay.portal.util.PortalUtil; import com.liferay.portal.model.User; User user = PortalUtil.getUser(actionRequest ); out.println("First Name :"+user.getFirstName()); out.println("Last Name :"+user.getLastName()); out.println("Full Name :"+user.getFullName());
import com.liferay.portal.kernel.util.ReleaseInfo; String version = ReleaseInfo.getVersion(); out.println("The Liferay Version :"+version);
import com.liferay.portal.theme.ThemeDisplay ; import com.liferay.portal.util.WebKeys; import com.liferay.portal.model.Theme; ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY); Theme theme = themeDisplay.getTheme(); out.println("Current Theme: "+theme.getName());
Limitation:
This feature can be used only for Liferay API as well as liferay default services. You will not be able to test your own custom services which are developed as plugin portlet.