Reputation: 60203
In Liferay 7, I have a Web Content, a vocabulary and a category.
How to set the category to the Web Content?
I wrote this code:
article = JournalArticleLocalServiceUtil.addArticle(...);
category = AssetCategoryLocalServiceUtil.addCategory(...);
AssetCategoryLocalServiceUtil.setAssetEntryAssetCategories(
article.getPrimaryKey(), new long[]{category.getPrimaryKey()});
At execution no error, but the category does not show up on the edit page of the created Web Content:
The category has been created successfully, but the Web Content does not get that category assigned.
What am I doing wrong?
I have also tried addAssetEntryAssetCategories
, addAssetEntryAssetCategory
, addAssetCategoryAssetEntry
: same problem.
Upvotes: 2
Views: 1650
Reputation: 1443
I am using liferay 7.1 dxp
In my case I have to update category of journal article or web content using program. In order to achieve this I have to use assetEntryAssetCategoryRel class. to access this and related class first I added dependency to my build.gradle file
compileOnly group: "com.liferay", name: "com.liferay.asset.entry.rel.api", version: "1.1.0"
List<AssetEntryAssetCategoryRel> assetEntryAssetCategoryRelsByAssetEntryId = AssetEntryAssetCategoryRelLocalServiceUtil.
getAssetEntryAssetCategoryRelsByAssetEntryId(assetEntry.getEntryId());
if(assetEntryAssetCategoryRelsByAssetEntryId!=null && !assetEntryAssetCategoryRelsByAssetEntryId.isEmpty()){
AssetEntryAssetCategoryRel assetEntryAssetCategoryRel = assetEntryAssetCategoryRelsByAssetEntryId.get(0);
assetEntryAssetCategoryRel.setAssetCategoryId(assetCategory.getCategoryId());
assetEntryAssetCategoryRel = AssetEntryAssetCategoryRelLocalServiceUtil.updateAssetEntryAssetCategoryRel(assetEntryAssetCategoryRel);
}
I had assetentry and assetcategory object This works fine for me
Upvotes: 0
Reputation: 1215
Try using any of these 2 functions to add category:
addAssetEntryAssetCategory(long entryId, long categoryId);
addAssetEntryAssetCategories(long entryId, long[] categoryIds);
In your code, you are using primary_key, however, as per documentation you should be using entry id and category id. So your function call should look like this:
AssetEntry entry = AssetEntryLocalServiceUtil.fetchEntry(JournalArticle.class.getName(), article.getResourcePrimKey());
AssetCategoryLocalServiceUtil.addAssetEntryAssetCategory(
entry.getEntryId(), category.getCategoryId());
Since 7.0, they removed the getEntryId method from JournalArticle you would need an additional call to fetch it. There is a update
method which you may also consider that would do this in single call. I'm still using 6.2 and catching up 7 :).
Please note categories are designed for use by administrators, not regular users.
Upvotes: 2