Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save darong-khalibre/23807b550e7a1c6257ee5aefa66dec0c to your computer and use it in GitHub Desktop.
Save darong-khalibre/23807b550e7a1c6257ee5aefa66dec0c to your computer and use it in GitHub Desktop.
Liferay Javascript API usage and examples

#TOC

  • Invoking Liferay Services - Liferay.Service

Single Request

Liferay.Service(
        '/user/get-user-by-email-address',
        {
                companyId: Liferay.ThemeDisplay.getCompanyId(),
                emailAddress: '[email protected]'
        },
        function(obj) {
                console.log(obj);
        },
        function(ex) {
                console.log('exception: ' + ex);
        }
);

or

Liferay.Service(
        { '/user/get-user-by-email-address': {companyId: Liferay.ThemeDisplay.getCompanyId(),
                                              emailAddress: '[email protected]'}
        },
        function(obj) {
                console.log(obj);
        },
        function(ex) {
                console.log('exception: ' + ex);
        }
);

Batch Request

Liferay.Service(
        [
                {
                        '/user/get-user-by-email-address': {
                                companyId: Liferay.ThemeDisplay.getCompanyId(),
                                emailAddress: '[email protected]'
                        }
                },
                {
                        '/role/get-user-roles': {
                                userId: Liferay.ThemeDisplay.getUserId()
                        }
                }
        ],
        function(obj) {
                // obj is now an array of response objects
                // obj[0] == /user/get-user-by-email-address data
                // obj[1] == /role/get-user-roles data

                console.log(obj);
        },
        function(ex) {
           console.log('exception: ' + ex);
        }
);

Nesting Request

Liferay.Service(
        {
                "$user = /user/get-user-by-id": {
                        "userId": Liferay.ThemeDisplay.getUserId(),
                        "$contact = /contact/get-contact": {
                                "@contactId": "$user.contactId"
                        }
                }
        },
        function(obj) {
                console.log(obj);
        },
        function(ex) {
           console.log('exception: ' + ex);
        }
);

Synchronous Request

Not recommend to use on main thread.

Liferay.Service(
        {'/user/get-user-by-email-address': {companyId: Liferay.ThemeDisplay.getCompanyId(),
                                             emailAddress: '[email protected]'},
         'io': {'sync': true}       
        },
        function(obj) {
                console.log(obj);
        },
        function(ex) {
           console.log('exception: ' + ex);
        }
);

Tested Environment

  • 6.2

Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment