Sunday 31 August 2014

Import Roles in Liferay 6.1


In previous post, we saw how to export Roles in Liferay 6.1 via xml.

Below is the code snippet for importing Roles along with Permissions in your Liferay Portal.

Export Roles in Liferay 6.1

Many a times, it is required to have Import/Export functionality for Roles.
This functionality is already supported in Liferay 6.2 where we can do lar import/export for roles and organizations as well.

Below is the code snippet for Exporting Roles along with Permissions in the form of xml in Liferay 6.1.

Create a hook in liferay eg, RolesMgmt hook. Create a class called as ExportRolesAction which extends BaseStrutsPortletAction.

Refer below code for the same:




Now, you can override view.jsp from path - /portal-web/docroot/html/portlet/role_admin/view.jsp
and add code to display buttons for Import Roles and Export Roles on front end.

So in addition to the existing code of view.jsp, you can add below code snippet for the import/export:


You can also add code for validating the checkboxes like disabling the checkboxes of default role types. In the next post I shall be giving sample code for Roles Import.

Thanks for reading!
Happy Learning!

Wednesday 20 August 2014

Using Custom Fields/Custom Attributes in Liferay 6.1


Some time we may come across the need for adding a property or storing data associated with OOTB portlets. We dont need to create any new service. It can be done with the help of Custom Attributes or Custom Fields. Liferay provides this feature through Expando services.

The mechanism is supported by using 4 database tables: ExpandoTable, ExpandoColumn, ExpandoRow and ExpandoValue.

Expando values can be added to liferay objects like: Users, Documents and Media, Web Contents, Bookmarks, etc. through control panel and programmatically to the custom portlets as well.

For Custom-Portlets:

Create custom-fields for custom-table:
Suppose we want to create the custom fields into custom-table. e.g Let us create a custom field named
PrimaryTag and associate it with a custom portlet named VideoManagement.
Example code:

It will add an entry in all 4 expando-tables mapped with VideoManagement classname.


Display custom-fields into your webpage(for custom portlet)
Liferay provides below code to display custom fields on page for all the OOTB portlets.
Same code can be used in custom-portlet to display them as text fields.


In LocalServiceImpl add following code for storing expandovalue:


The way of setting value:


e.g addCustomAttribute(_companyId, _className, PrimaryTag", _classPK, “primaryTagValue”);
i.e. ExpandoValueLocalServiceUtil.addValue(classNameId, tableId, columnId, classPK, data);


The way of getting value:


Expando Table:

Expando Column:


Expando Value:

where tableId is the id of default table, columnId the id of retrieved column and classPK the primary key of entity.

For OOTB portlets e.g journalArticle, we have to first create custom attributes from control panel.

The following code sets attributes for particular article using service context.


Thanks for reading! Happy Learning!

Using Asset Category in Custom Portlet of Liferay 6.1

Many a times it is required that when one of our custom content is created we need to let the asset framework know. You just need to invoke a method of the asset framework. When invoking this method we can also let the framework know about the tags and/or categories of the content that was just authored.

This is the code you can put it to any jsp page for implementing category popup.

// vManag is the object which is refering to custom portlet named VideoManagement.

In JSP



The tag <liferay-ui:asset-categories-selector> uses AssetCategoryServiceUtil.getCategories(className, classPK) to get the list of categories and populates the curCategoryIds from this list.

All the methods that you will need to invoke are part of the AssetEntryLocalService. In particular you should access these methods using either the static methods of AssetLocalServiceUtil or by using an instance of the AssetEntryLocalService injected by Spring.

First time you are adding record in custom table so you have to select cateogry from popup.It will show all categories in the category popup by specifying empty string in curCategoryIds and At the time of editing record you have to get categoryIds [like 45326,45327] from custom table that is already stored after adding record and then assign categoryIds to curCategoryIds in asset-categories-selector tag.

So you can see the category selected and if 'select' button is clicked, the same category is shown as checked and that will be useful when you editing any record of custom-table to show default selected values in that popup.

When user select any category from that popup. It will automatically create a hidden field with name assetCategoryIds in the same page.


In Java class

So here you can get the values of selected categoryIds like 45326,45327[two category selected in popup]

you can make entry in custom table for adding/editing record and also in assetEntry table for that same recordId.

This is the logic for making entry in assetEntry table for that recordId.


userId :- is the identifier of the user who created the content.

VideoManagement.class.getName() :- which is the class name refering to custom portlet named VideoManagement.

vManag.getVideoId() :- represent as classPK which is unique recordId for that content[specify Id of that record which is already added/updated].

assetCategoryIds :- represent the categories that have been selected by the author of the content.

Url :- which specify the url of content i.e for videomanagement portlet you can specify videoURL.

Thanks for reading! Happy Learning!

Refer more,
https://www.liferay.com/documentation/liferay-portal/6.1/development/-/ai/asset-framewo-4