How to retrieve images associated with Asset Categories from custom code?


  • When editing a category, it is possible to set images.
  • However, when trying to get those images from my custom code, I am unable to do that. There are no related methods in AssetCategory objects.


  • Liferay DXP 7.4


  • These images are handled by Liferay Commerce's modules instead of other modules related to the Asset Framework where Asset Categories belong to.
  • That's why it is not as easy as access other categories' fields.
  • In order to retrieve the images, you would have to use the Commerce API. For example, calling CPAttachmentFileEntryServiceUtil.getCPAttachmentFileEntries(). With this, you can call fetchFileEntry() on the CPAttachmentFileEntry objects to get the associated FileEntry.
  • Find below a sample snippet (tested in 7.4 u 76):
    import com.liferay.account.constants.AccountConstants
    import com.liferay.asset.kernel.model.AssetCategory
    import com.liferay.commerce.product.model.CPAttachmentFileEntryConstants
    import com.liferay.commerce.product.service.CPAttachmentFileEntryServiceUtil
    import com.liferay.document.library.util.DLURLHelperUtil
    import com.liferay.portal.kernel.theme.ThemeDisplay
    import com.liferay.portal.kernel.util.PortalUtil
    import com.liferay.portal.kernel.workflow.WorkflowConstants

    def assetCategoryId = 000

    def themeDisplay = (ThemeDisplay)actionRequest.getAttribute(com.liferay.portal.kernel.util.WebKeys.THEME_DISPLAY)

    def cpAttachmentFileEntries = CPAttachmentFileEntryServiceUtil.getCPAttachmentFileEntries(
    PortalUtil.getClassNameId(AssetCategory.class), assetCategoryId,
    CPAttachmentFileEntryConstants.TYPE_IMAGE, WorkflowConstants.STATUS_ANY, -1, -1)

    for (def cpAttachmentFileEntry in cpAttachmentFileEntries) {
    def fileEntry = cpAttachmentFileEntry.fetchFileEntry()
    out.println("fileEntry " + fileEntry.getFileName())

    def thumbnailSrc = DLURLHelperUtil.getThumbnailSrc(fileEntry, themeDisplay)
    out.println("thumbnailSrc " + thumbnailSrc)

    // You can also leverage CommerceMediaResolverUtil
    def url = CommerceMediaResolverUtil.getURL(AccountConstants.ACCOUNT_ENTRY_ID_ANY,
    out.println("url " + url)


Additional Information

  • Some related feature requests:
    • LPD-924 Extend the taxonomy / categories to use the friendly URL and image across DXP (API Headless)
    • LPD-152 As a Site Administrator, I would like to have Categories' images without dependencies to Liferay Commerce modules
0 人中有 0 人觉得有帮助