Liferay Logo

Determine latest version of Web Content in Liferay

Most of the time when using Liferay’s Web Content (i.e. JournalArticle) you will want to determine the latest version to be displayed to your users. The following code snippet shows a simple solution to do so:

List<JournalArticle> articles = JournalArticleLocalServiceUtil.getStructureArticles(GROUPID, STRUCTUREID);
ListIterator<JournalArticle> it = articles.listIterator();
List<String> checkedArticleIds = new ArrayList<String>();

while (it.hasNext()) {
  JournalArticle article = it.next();

  if (checkedArticleIds.contains(article.getArticleId())) {
    continue; // previous article version already checked
  }

  JournalArticle articleLastVersion = JournalArticleLocalServiceUtil.getLatestArticle(GROUPID, article.getArticleId());

  checkedArticleIds.add(article.getArticleId());

  System.out.println("Added articleId " + article.getArticleId() + " with version " + article.getVersion());
}

3 thoughts on “Determine latest version of Web Content in Liferay

  1. mojtaba safaeian

    But this is not optimised way to do this, what if we have tens of thousands of articles, you must offer a way to query database for latest article versions not to retrieve all articles and then select the latest IDs.

    thank you

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.