Monday, April 16, 2012

Liferay : How to call another portlet of another page

In Liferay you may need to call another portlet of another page. Lets see how we can communicate between pages and portlets.

Please follow below mentioned steps for portlet communication accross pages.

1. Add tag library as shown below

<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet"%>

2. Import below mentioned classes

<%@ page import='com.liferay.portal.theme.PortletDisplay' %>
<%@ page import='com.liferay.portal.theme.ThemeDisplay' %>
<%@ page import='com.liferay.portal.kernel.util.WebKeys'%>


3. Get portlet id and portlet name of the JSP which need to be called. Add below codes to the JSP which need to be called.

Note : You may need to save portlet id and portlet name in session, property file etc and pass those values in calling JSP.

<%
 ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
 PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
 long portletId = themeDisplay.getPlid();
 String portletName = portletDisplay.getId();

%>


4.  Create renderURL. Get portlet id and portlet name from stored session, property file etc and pass here for creating render url.

<liferay-portlet:renderURL var="URLName" plid="<%= portletId %>" portletName="<%= portletName %>" >
<liferay-portlet:param name="jspPage" value="/html/portletName/jspFile.jsp" />
<liferay-portlet:param name="param1" value="<%= param1 %>"></liferay-portlet:param>
<liferay-portlet:param name="param2" value="<%= param2 %>"></liferay-portlet:param>
</liferay-portlet:renderURL>


<a href="<%= URLName %>">Click Here</a>