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

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