How to get Portlet Id using the portlet name in Liferay?
Asked Answered
H

2

10

I have a requirement of getting resource permission using portlet name.

I will have the name of the portlet not the Id.

Resource Permission name for a portlet is that portlet's Id. I checked the Portlet table, it has only the Id and other info. Where will be the other attributes of portlet saved?.

Is there a way I can get portlet's Id by using portlet's name. I have a workaroud to get all portlets and compare, but if I can directly get portlet's Id using portlet's name it will be helpful.

Its urgent, if anyone knows pls reply back, it will be of great help.

Thanks in Advance

Historicism answered 22/6, 2012 at 6:15 Comment(0)
U
13

If you look closely as to how the Portlet Id is generated based on the Portlet Name, you would get the answer.

It is a constant pattern which is followed, so you can construct the Portlet Id like liferay does it if you have the name. Liferay also constructs the ID with the help of the portlet name.

Pattern of portlet-id: <portletname in xml>_WAR_<servlet context name i.e. the war file without any special characters such as the hyphen>

For example: If your portlet name is MyWork defined in portlet.xml and the generated file in the webapps folder (if you are using tomcat) is MyWork-portlet then the resultant Id will be MyWork_WAR_MyWorkportlet.

Now if you have liferay source code, you can look at PortletLocalServiceImpl's private _readPortletXML method.

Edit:
If you want to find the portlets on a particular page (given the friendly-url of the page) then you may find this answer helpful.

Unlovely answered 23/6, 2012 at 8:47 Comment(7)
Thank You Prakash. But how will I know the servlet context name which is war file name if i have multiple wars deployed. I did know how the portlet id is constructed. I will have many portlets part of different servlet contexts.Historicism
Multiple wars of the same portlet on the same instance? or One war with many portlets packaged? If you can elaborate about the environment it would be helpfulUnlovely
One war with multiple portlets packaged which are instancible. I have also observed that portlet id will have instance info appended. how to handle in that case. If i deploy it in different servers the instance id will be different.Historicism
@Vijayalakshmi: For one war multiple portlets my answer is fine. Now instance info is totally a different case. Since instance info is page/layout related and hence is not stored in the Portlet table and is stored in Layout table's typeSettings column. You can edit your question to include that you want instance info as well, that is not at all clear from your question.Unlovely
@Vijayalakshmi: Currently I can only think of string manipulation on the typeSettings column as a hack, since the instance is nothing but a random string attached. May be you can dig more in liferay's source code as to how the instance is generated and how liferay identifies it to store PortletPreferences.Unlovely
Thank you prakash. I will look into and get back to you after looking into it.Historicism
-to verify connect to liferay DB porltet table and check the portletid colume- SELECT * from portlet ; I found that in liferay 6.1 it is <portletname without -._>_WAR_<deployed war file name - under webapps in tomcat , again withour -._>Decahedron
M
5

You can try this:

System.out.println("ID : " + themeDisplay.getPortletDisplay().getId());

System.out.println("InstanceID: " + themeDisplay.getPortletDisplay().getInstanceId());

System.out.println("Portlet Name: " + themeDisplay.getPortletDisplay().getPortletName());

Don't forget:

<% ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
    themeDisplay.getUser().getScreenName(); %>
Melburn answered 8/7, 2015 at 13:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.