Monday, July 19, 2010

Quick Tips

Tips
For using Liferay services in Web Content portlet Previously known as journal content portlet , we use LR services in templates
e.g. #set ($userLocalService = $serviceLocator.findService("com.liferay.portal.service.UserLocalService"))

However at first attempt we encounter null which happens when we forget to update the property in portal-ext.properties.
Use of LR services are restricted by default in templates.
Following property has list of restricted variables
so change property from journal.template.velocity.restricted.variables=serviceLocator
journal.template.velocity.restricted.variables=
Once serviceLocator is available we can invoke the services to achieve desired results.
===================================================
Get httpRequest within the portlet
HttpServletRequest httpRequest = PortalUtil.getHttpServletRequest(actionReq);

===================================================
CMS Template VM code for finding user and roles

This code has been used by developers time and again I have used some different methods than existing approach to make it simpler.

#set ($userLocalService = $serviceLocator.findService("com.liferay.portal.service.UserLocalService"))
#set ($roleLocalService = $serviceLocator.findService("com.liferay.portal.service.RoleLocalService"))
#set ($user_id = $request.getAttribute("USER_ID"))
#set ($user = $userLocalService.getUserById($user_id))
#set ($role = $roleLocalService.getRole($user.getCompanyId(),"sponsor"))
#set ($hasUserRole = $roleLocalService.hasUserRole($user.getUserId(),$role.getRoleId()))

======================================================

4 comments:

Niraj said...

Hi Abhishek

Your post has helped me better understand how Velocity works within liferay. But I am still not sure where to put the code 'HttpServletRequest httpReq ....'. I not developing a custom portlet. I am trying to set the value of a structure text field to the screen name of the currently signed in user. Please help.

kanu said...

hi,
#set($layoutLocalService = $serviceLocator.findService("text-resolver-portlet","com.aonhewitt.portal.core.textresolver.service.TextContentLocalServiceUtil"))
#set($user = $serviceLocator.findService("com.liferay.portal.service.UserLocalService"))
#set($language = $request.get("theme-display").get("language-id"))
#set($clientcontent.data = $layoutLocalService.getText($user, "base", "global", "msIETM", $language))


$clientcontent.data


thi is my vm. but i am not getting any output. please help

Abhishek Saxena said...

Hi Niraj , use the $user object .
Please see the details in the below mentioned post .
http://abhishek-liferay.blogspot.in/2010/12/velocity-templating-and-its-application.html

Abhishek Saxena said...

Hi Kanu,
I think you should enable the service access

TO enable access of services in liferay make following changes in portal-ext.properties
# Input a comma delimited list of variables which are restricted from the
# context in Velocity based Journal templates.
#
journal.template.velocity.restricted.variables=serviceLocator
remove serviceLocator in property it will now look as
journal.template.velocity.restricted.variables=

example --- (a complete example is available in my earlier post named quick tips)
#set ($userLocalService = $serviceLocator.findService("com.liferay.portal.service.UserLocalService"))
#set ($roleLocalService = $serviceLocator.findService("com.liferay.portal.service.RoleLocalService"))