Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • Using Javascript to access liferay services

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.48k
    Comment on it

    The Below code will show you how you can create the service in Liferay and how you can call that services using javascript.

    1. For this, First you have to create service builder(service.xml) to build service using ant build-service command.


    <!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.2.0//EN"          "">
    <service-builder package-path="com.evon">
        <entity name="Employee" local-service="true" remote-service="true">
            <!-- PK fields -->
            <column name="empId" type="long" primary="true" />
            <!-- Other fields -->
            <column name="empname" type="String" />
            <column name="empaddress" type="String" />
            <!-- Order -->
            <order by="asc">
                <order-column name="empname" />
    1. After successful build of service.xml file open EmployeeServiceImpl and create your own service method.
    public class EmployeeServiceImpl extends EmployeeServiceBaseImpl {
                  public  Employee getEmployee(long emplyeeId)
                          throws com.liferay.portal.kernel.exception.PortalException,
                                      com.liferay.portal.kernel.exception.SystemException {
                          return EmployeeLocalServiceUtil.getEmployee(emplyeeId);

    Then we need to run service builder using ant build-service command or from eclipse ant view we can run same command.

    1. To Access your Service using the JavaScript you can write the code in your view.jsp


    <%@ taglib uri="" prefix="aui" %>
        <%@ taglib uri="" prefix="portlet" %>
        <portlet:defineObjects />
        <script src=""></script>
        Liferay.provide(window,'getDetails',function() {
                        emplyeeId: $('#<portlet:namespace />empId').val()
                      function(obj) {
        <aui:input type="text" id="empId" name="empId" />
        <aui:button type="button" value="Click" onClick="getDetails();" />
    1. Now Deploy that portlet in liferay server.
      Note: In my case the portlet name is JSONServiceDemo-portlet.

    Finally enter the employee Id in text box then click on button it can return the employee detail in JSON format.

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: