Liferay 6: how to get web contents by category

Below is the Java snippet to get the list of web contents which belong to a specific category.

public List<JournalArticle> getArticlesByCategory(AssetCategory category) throws SystemException {

	// Build query
	AssetEntryQuery query = new AssetEntryQuery();
	query.setAllCategoryIds(new long[] { category.getCategoryId() });
	query.setExcludeZeroViewCount(false);

	// Run query
	List<AssetEntry> assets = AssetEntryLocalServiceUtil.getEntries(query);

	// Build result
	List<JournalArticle> result = new ArrayList<JournalArticle>();
	for (AssetEntry asset : assets) {
		try {
			result.add(JournalArticleLocalServiceUtil.getLatestArticle(asset.getClassPK(), WorkflowConstants.STATUS_APPROVED));
		} catch (PortalException ignored) {
			// happens if the asset has no JournalArticle associated ==> should not happen
		}
	}

	return result;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s