Accessing a custom service / liferay service would be essential specially if you are dealing with the .vm file in Theme / Template (Webcontent)
There are four simple steps to access service inside .vm file:
1) Changes in portal-ext.properties
2) Modify .vm file to access portal service
3) Modify .vm file to access portlet (custom service)
4) Verify
Here are the steps in detail:
1) Changes in portal-ext.properties
To access service we need to use the velocity custom field called "serviceLocator" but by default we can not use service inside .vm file.
as "serviceLocator" variable is restricted to use in .vm file
Here is the default configuration of the liferay inside portal-impl/src/portal.properties
#
# Set a comma delimited list of variables the Velocity engine cannot
# have access to. This will affect Dynamic Data List templates, Journal
# templates, and Portlet Display templates.
#
velocity.engine.restricted.variables=serviceLocator
To enable use of "serviceLocator" below line need to be placed inside porta-ext.properties file.
velocity.engine.restricted.variables=
2) Modify .vm file to access portal service
Lets say if I want to find company id of one of the existing webcontent ("JournalArticle") then here is the code I may write. Service provided by Liferay OOTB (Out of the Box)
#set ($customLocalService = $serviceLocator.findService('com.liferay.portlet.journal.service.JournalArticleLocalService'))
$customLocalService
Company Id : $customLocalService.getArticle(22214).getCompanyId();
3) Modify .vm file to access portlet (custom service)
Lets say I have my own portlet called "student-service-portlet" and I like to access name of student having student id "45" then here is the code I would write.
#set ($customLocalService1 = $serviceLocator.findService('student-service-portlet','com.myschool.entities.service.StudentLocalService'))
$customLocalService1
Student Name is : $customLocalService1.getStudentById(45).getName();
4) Verify
To verify our functionality we will make use of Webcontent , Structure, Template.
Just create one web content with one dummy structure and a template associated to it.
Select the language of Template as "Velocity (.vm)" and paste the code written in step #2 / #3
Save Webcontent and add "Web Content Display" portlet and select the newly created webcontent in it.
On screen you will see the output as
com.liferay.notifications.hook.service.impl.JournalArticleLocalServiceImpl@5e7eb702 Company Id : 20155; com.myschool.entities.service.impl.StudentLocalServiceImpl@49a78571 Student Name is : Henal;
We are done!
Cheers!
Henal Saraiya
There are four simple steps to access service inside .vm file:
1) Changes in portal-ext.properties
2) Modify .vm file to access portal service
3) Modify .vm file to access portlet (custom service)
4) Verify
Here are the steps in detail:
1) Changes in portal-ext.properties
To access service we need to use the velocity custom field called "serviceLocator" but by default we can not use service inside .vm file.
as "serviceLocator" variable is restricted to use in .vm file
Here is the default configuration of the liferay inside portal-impl/src/portal.properties
#
# Set a comma delimited list of variables the Velocity engine cannot
# have access to. This will affect Dynamic Data List templates, Journal
# templates, and Portlet Display templates.
#
velocity.engine.restricted.variables=serviceLocator
To enable use of "serviceLocator" below line need to be placed inside porta-ext.properties file.
velocity.engine.restricted.variables=
2) Modify .vm file to access portal service
Lets say if I want to find company id of one of the existing webcontent ("JournalArticle") then here is the code I may write. Service provided by Liferay OOTB (Out of the Box)
#set ($customLocalService = $serviceLocator.findService('com.liferay.portlet.journal.service.JournalArticleLocalService'))
$customLocalService
Company Id : $customLocalService.getArticle(22214).getCompanyId();
3) Modify .vm file to access portlet (custom service)
Lets say I have my own portlet called "student-service-portlet" and I like to access name of student having student id "45" then here is the code I would write.
#set ($customLocalService1 = $serviceLocator.findService('student-service-portlet','com.myschool.entities.service.StudentLocalService'))
$customLocalService1
Student Name is : $customLocalService1.getStudentById(45).getName();
4) Verify
To verify our functionality we will make use of Webcontent , Structure, Template.
Just create one web content with one dummy structure and a template associated to it.
Select the language of Template as "Velocity (.vm)" and paste the code written in step #2 / #3
Save Webcontent and add "Web Content Display" portlet and select the newly created webcontent in it.
On screen you will see the output as
com.liferay.notifications.hook.service.impl.JournalArticleLocalServiceImpl@5e7eb702 Company Id : 20155; com.myschool.entities.service.impl.StudentLocalServiceImpl@49a78571 Student Name is : Henal;
We are done!
Cheers!
Henal Saraiya