0
votes

I am trying to write a simple Liferay portlet in Python. The portlet will show a list of categories and when clicked will show a list of Web Content articles (journal articles) of a certain structure.

I am able to get the list of categories but cannot find a way using the liferay api to get a list of articles by category?

I have searched allover but it seems to me the method should be on this page:

http://docs.liferay.com/portal/6.1/javadocs/com/liferay/portlet/journal/service/JournalArticleLocalServiceUtil.html

3
Liferay stores its category-asset[article,blog,thread etc] relation in assetentry_assetcategories table, you could use AssetEntryQuery to fetch assets category wise.Pankaj Kathiriya

3 Answers

5
votes

It is a Java implementation but really easy to convert into python.

<%
String languageId = LanguageUtil.getLanguageId( renderRequest );
List<JournalArticle> journalArticleList = new ArrayList<JournalArticle>();

AssetEntryQuery assetEntryQuery = new AssetEntryQuery();
assetEntryQuery.setAnyCategoryIds(new long[] { 12704 }); //category Id
assetEntryQuery.setOrderByCol1("modifiedDate");
assetEntryQuery.setEnd(5);
List<AssetEntry> assetEntryList = AssetEntryLocalServiceUtil.getEntries(assetEntryQuery);
for (AssetEntry ae : assetEntryList) {
    JournalArticleResource journalArticleResource = JournalArticleResourceLocalServiceUtil.getJournalArticleResource(ae.getClassPK());
    JournalArticle journalArticle = JournalArticleLocalServiceUtil.getLatestArticle(journalArticleResource.getResourcePrimKey());


    JournalContentUtil.clearCache();
    String content = JournalContentUtil.getContent(journalArticleResource.getGroupId(), journalArticle.getArticleId(), "view", languageId, themeDisplay);

    out.println("<br>"+journalArticle.getTitle(languageId)+"<br>");
    out.println(content);

}
%>
1
votes

The proposed solution is good yet needs one extra piece. It will return all Assets - web-content-articles are a subset of assets. For example, you will get documents (which have been categorized the same way) as well. To refine your search, add a className, classNameid, or classTypeId to the AssetEntryQuery (in addition to the category id). Alternatively, within the for-loop you could pick out the web-content, ignore the others.

0
votes

Thanks, AssetEntryQuery was the solution:

from com.liferay.portlet.asset.service.persistence import AssetEntryQuery
from com.liferay.portlet.asset.service import AssetEntryServiceUtil

aq = AssetEntryQuery()
aq.setAllCategoryIds([442492])
articles = AssetEntryServiceUtil.getEntries(aq)

for a in articles:
  out.write(str(a.title))
  out.write(str(a))