Search code examples
parametersliferayportletportalliferay-6

How to parametrize portlets on the fly?


Portlets have an EDIT mode, and this is the way we can parametrize them.

My question is what it the best way to parametrize a portlet exactly at the moment its loaded, programmatically.

For example: I want to get Liferay to load two instances of the portlet with a certain ID, one with paramA, second with paramB.


Solution

  • To load different parameters (better known in Liferay as "Portlet Preferences") you can use PortletPreferences to store and retrieve different parameters,

    PortletPreferences preferences =
        PortletPreferencesFactoryUtil.getPortletSetup(
            request, portletId);
    

    The factory takes 2 parameters,

    • Request - The request.
    • PortletId - The ID of your portlet, (for example "name_WAR_myportlet_INSTANCE_ABCD").

    With this object you can get parameters:

    String myValue = preferences.getValue("my-value");
    

    And you can store values:

    preferences.setValue("my-value", "this-value");
    preferences.store();