AssetEntryQuery By Category, Tag, Type, and Keyword example | Liferay 6.1, 6.2

Liferay 6.1 or 6.2 using AssetEntryQuery to search by Category, Tag, Type, and Keyword.  This example will allow you to query any asset type (web content, document, blog post, message board post, custom assets, etc..) by Category, Tag, Type, and Keyword.  Possible applications for this code would be using it for a custom search portlet on your site or if you need to grab a certain subset of articles to display for a news item section of your site but don’t want to use the default asset publisher.

 

private List<AssetEntry> getArticlesByCategory(long groupId, int limit,long[] allCategoryIds,long[] allTagIds, int start) {
    	List<AssetEntry> results = null;
    	
    	try {
    		int end = limit;
    		start = start*limit;
    		end = start+limit;
    		
    		long[] allGroupIds = {groupId};//change this to multiple group Ids if you want to query multiple sites
    		AssetEntryQuery assetEntryQuery = new AssetEntryQuery();
    		
    		assetEntryQuery.setAnyCategoryIds(allCategoryIds);//this is used to query assets by categories
                assetEntryQuery.setAnyTagIds(allTagIds);//this is used to query assets by tags
    		assetEntryQuery.setVisible(true);
    		assetEntryQuery.setEnablePermissions(false);
    		assetEntryQuery.setClassName("com.liferay.portlet.journal.model.JournalArticle");//this is filtering only by web content. 
    		assetEntryQuery.setGroupIds(allGroupIds);
    		assetEntryQuery.setStart(start);
    		assetEntryQuery.setType("type");//here is where you can set the type
    		assetEntryQuery.setEnd(end);
    		assetEntryQuery.setOrderByCol1("viewCount");
    		assetEntryQuery.setOrderByType1("DESC");
    		assetEntryQuery.setOrderByCol2("title");
    		
    		results = AssetEntryServiceUtil.getEntries(assetEntryQuery);
    		
    	} catch (Exception e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
    	
    	return results; 
    }

Comments

comments