Blogs

Blogs

Nesting Liferay JSON Service calls

One really cool feature of Liferay JSON web services is the ability to nest service calls.

For example, what if you would like to update a web content journal article via JSON? The /journalarticle/update-article method requires the following four parameters: 

 

Parameter Description
groupId ID for the Liferay site
articleId ID for the web content journal article
content XML representation of the web content
version Article version number

groupId, articleId, and content are easy to come up with. But how can I find the article version number? I could make a separate call to get-article to retrieve the article metadata including the version number, but this would add extra complexity (and an extra network call) to my client code. 

Instead it is also possible to automatically look up the version number while updating with a nested call like this: 

 var cmd = {
            "$article = /journalarticle/get-article": {
                "groupId": article.groupId,
                "articleId": article.articleId,
                "$update = /journalarticle/update-article": {
                    "@version": "$article.version",
                    "groupId": article.groupId,
                    "articleId": article.articleId,
                    "content": xml,
                    "serviceContext.scopeGroupId": article.groupId
                }
            }
        };

How cool!

Thank you for your article, it's a usefull feature not known by most users.

For those who seek more, there are some examples in the official tutorial : https://dev.liferay.com/develop/tutorials/-/knowledge_base/6-2/json-web-services-invoker#nesting-service-calls