Wednesday 30 August 2017

How to Access Liferay Services in Velocity(VM) files using Service Locator?



Some time we need to access Liferay Services in our theme or Web content Templates. So today we will discuss how to access Liferay Services in our theme and templates. 

For Theme
If you want to access liferay services like user services in your theme you can use this code in portal_normal.vm:-

#set($userLocalService = $serviceLocator.findService('com.liferay.portal.service.UserLocalService'))
=>$userLocalService.getUsersCount()
view rawportal_normal.vm hosted with ❤ by GitHub


Explanation:-
Here we first get the UserLocalService object and then callgetUsersCount method.
For Web Content Templates
You can use the above code in your templates files but you need to do one extra step. Just enter :-

velocity.engine.restricted.variables=

in your portal-ext.properties and restart server

No comments:

Post a Comment