Tips & Tricks - Filtering Portlets with Groovy

This post uses a simple example to show how Groovy scripts can be used to control the display and integration of portlets on the portal page.

Where can Groovy scripts be used to display and select portlets?

A Groovy script for restricting the display and selection of portlets can be used in the Properties dialog in the following locations:

  • "Portlet Container" Application Element

  • View Page / Tab Portlet

  • Application Properties / Portlet Tab

You can find more information here.

Displaying and Selecting Portlets Without Filtering

In a simple application created using the "Basic Application" template, values are entered and displayed in the view table on the application's home screen.

This page is configured for use as a portlet. The "Use portlet in other applications" setting is also enabled so that the portlet can be selected on the portal's home page.

In the browser, the "Values" portlet can now be selected on a portal page under "Portlets from Applications" and made available in any container using drag-and-drop. You can find general information on this topic here.

The portlet is displayed for all users.

Restrict the selection and/or display of the portlet on the web using a Groovy script

With the following script, the portlet is displayed only if the table contains records and the "Restrict portlet display on the web via Groovy script" setting is enabled. The script can also be used for the setting "Restrict display of the portlet on the web using a Groovy script"—in that case, the portlet will no longer appear in the portlet settings under "Portlets from Applications" and therefore cannot be embedded on the portal page.

            g_dataCollection.getDataRange("768A3C783F2BC9FD7EF20B1979FD3B27498779E2").getNumberOfRecords() > 0
        

Replace the GUID in this script with the <tablerecords> GUID for your view table.

To do this, select the "Values" page and then use the "Show Elements" option in the main menu to display the elements in the application structure. Expand the structure all the way down to the table and then to the "tablerecords" entry. Double-click the "tablerecords" entry to open its Properties dialog box. You'll find the GUID there on the Expert tab.

Find out here how to use the script with the appropriate setting in the editor.