Tips & Tricks - Filtering portlets with Groovy
This article uses a simple example to show you how you can use Groovy script to control how portlets are displayed and integrated on portal pages.
Where can Groovy script be used to restrict the display and selection of portlets?
Groovy script can be used to restrict the display and selection of portlets in the properties dialog of the following:
-
Application element "Portlet container"
-
View page / ""Portlet"" tab
-
Application properties / ""Portlet"" tab
Further information can be found here.
Display and selection without a filter
In a simple application created with the "Basic application" template, values are recorded and displayed in the view table on the start page of the application.
This page is set for use as a portlet. The setting "Also use portlet in other applications" is also set so that the portlet can be selected on the start page of the portal.
In the browser, the "Values" portlet can now be made available on a portal page under "Portlets from applications" selected by drag & drop in any container. General information on this topic can be found here.
All users can now see the portlet.
Restrict the selection and/or display of the portlet in the browser with Groovy script
With the following script, the portlet is only displayed if the table contains data records and the setting "Restrict display of the portlet on the web by Groovy script" is set. The script can also be used for the setting "Restrict display of the portlet on the web using Groovy script" - then the portlet is no longer listed in the portlet settings under "Portlets from applications" and therefore cannot be integrated on the portal page.
g_dataCollection.getDataRange("768A3C783F2BC9FD7EF20B1979FD3B27498779E2").getNumberOfRecords() > 0
Replace the GUID in this script with the <tablerecords> GUID of your view table.
To do this, select the "Values" page and then show the elements in the application structure via the "Show elements" main menu. Open the structure down to the table and then to the "tablerecords" entry. Double-click on the "tablerecords" entry to open the properties dialog. There you will find the GUID on the Expert tab.
You can find out how the script can be used with the corresponding setting in the editor here.