Search code examples
javaliferay-7

How to create subcategory under each category in Liferay DXP programmatically?


In my program, we have already created vocabulary and under each vocabulary, we have created categories but I am trying to modify the code such that we can create subcategories under each categories and the subcategory will be unique.

I can add a subcategory under each category using Liferay but not sure how can I add them programmatically? Are there any methods or classes I can use to add them programmatically? Any help or hints would be appreciated

I am using Liferay DXP and Java 1.8


Solution

  • I'm not quite sure what exactly you need to do as you seam to ask for 2 different things.

    To add a category programaticaly you need to get a reference to AssetCategoryLocalService and call one of the addCategory methods. Here is an example from a test class:

    AssetCategory assetCategory = assetCategoryLocalService.addCategory(
                TestPropsValues.getUserId(), _group.getGroupId(), title,
                _assetVocabulary.getVocabularyId(), serviceContext);
    

    To make sure the names are unique you may use model listener. There is code sample that shows how to do this for Layout but you may follow the same approach to create one for AssetCategory.