Blog Post

...

Invoke Liferay service externally

Hi there, in this post we’ll take a look at the problem of Liferay 7 service invocation from the external app. Liferay provides a set of core services to be invoked using REST calls. That means that one can perform CRUD ops (read/write etc) on some Liferay instance using any client outside. A full set of available out-of-the-box services can be found, e.g. here liferayhost.com/api/jsonws. At this page you’ll see a various number of available Liferay services. For example, we can use the following (and many others) service methods of JournalArticle:

image


Let’s call the update-article-translation from the external app. It has the following method description taken from the same jsonws page:

image

To get this method executed we need to find its parameter values. As a starting point, groupId (39931) and articleId (125411) can be retrieved from current article (web content) URL, e.g.:

http://somesite.dk/web/site/utils?p_p_id=dk_lw_portlet_Utils&p_p_lifecycle=2&p_p_state=normal&p_p_mode =view&p_p_resource_id =getRawArticle&p_p_cacheability=cacheLevelPage&p_p_col_id=column- 1&p_p_col_count=9&_dk_lw_portlet_Utils_groupId=39931 &_dk_lw_portlet_Utils_articleId=125411&_dk_lw_portlet_Utils_portletData =&_dk_lw_portlet_Utils_currentURL=%2Fweb%2Ftest-site%2Futils&_dk_lw_portlet_Utils_doAsUserId =&_dk_lw_portlet_Utils_dataType=JSON&_dk_lw_portlet_Utils_cmd=add

Also to make calls to Liferay’s web services, need to use Basic Authentication (wiki). To make it work we need to encode our login/password using Base64 algorithm. It’s possible to encode our credentials e.g. here (https://www.blitter.se/utils/basic-authentication-header-generator/):

image

Now we can use any REST client to make a call to Liferay to get the content of some web content. This endpoint should be used according to jsonws method description: /api/jsonws/journal.journalarticle/get-article. An example is made in ARC (Chrome Extension):

image

We’ll get a JSON response, from which we extract web content’s version and a content actually:

"version": 1,

"content": "<?xml version="1.0"?> <root available-locales="en_US" default-locale="en_US"> <dynamic-element name="content" type="text_area" index-type="keyword" instance-id="sjae"> <dynamic-content language-id="en_US"><![CDATA[<p>some content</p>]]></dynamic-content> </dynamic-element> </root>"

Now we should add a new translation to extracted content (note bold text below):

<?xml version="1.0"?>

<root available-locales="en_US, da_DK" default-locale="en_US">

<dynamic-element name="content" type="text_area" index-type="keyword" instance-id="sjae">

         <dynamic-content language-id="en_US"> <![CDATA[ <p>some content</p>]]> </dynamic-content>

         <dynamic-content language-id="da_DK"> <![CDATA[ <p>some content in Danish</p>]]></dynamic-content >

</dynamic-element>

</root>

Now we can use ARC client to make a call to Liferay to update the content of the web content. The following fields are required (“-” prefixed are skipped on Liferay side, but anyway we ought to add them to request):

  • groupId (39931)
  • articleId (125411)
  • version (value = 1, it should be the last version of the web content)
  • content (copy from 6.)
  • locale (set default locale e.g. «da_DK»), to skip use «-locale»
  • -description
  • -images
  • -title

Endpoint is, having POST method: /api/jsonws/journal.journalarticle/update-article-translation

An example made in ARC (Chrome Extension):

image

In the end the updated translation will be seen in Liferay UI:

image

That’s it! In this post we saw how to invoke Liferay service (get article and update article) from any external application using REST interface. I hope it was helpful.

Comments (1)

Tags: liferay


Comments:

...

amsaleka Mar 15, 2019 at 09:49 #

Nice blog..! I really loved reading through this article. Thanks for sharing such a amazing post with us and keep blogging...Also Checkout: cryptocurrency training in chennai | blockchain coaching in chennai | blockchain certification training in chennai | blockchain certification course in chennai

Leave a Comment