Wednesday, March 4, 2015

[Liferay] Get URL using Portlet ID

Hi,

Sometimes, we need to get url based on portlet id. All we have is a portlet, we can easily get portlet id and then find the page where it is added.

To generate a url like this, we need PortletRequest, portletId, plid, and phase of the portlet.

plid is layout id which can be get like this..

long plid = PortalUtil.getPlidFromPortletId(themeDisplay.getScopeGroupId(), true, portletId); 

In case you don't know how to get themeDisplay,

ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

Second argument to method is true which shows that this is private layout. In case of public layoue make it false. We already have portletId. 

We now have plid. Phase of the portlet is as per the requirement, assuming it is render phase for us. So once we have all the things. We call PortletURLFactoryUtil and create the url ( PortletURL )- 

PortletURLFactoryUtil.create(request, portletId, plid, PortletRequest.RENDER_PHASE);

That's it. Hope this helps.
Until next time. 

1 comment:

Note: Only a member of this blog may post a comment.