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

informatik01
  • 16,038
  • 10
  • 74
  • 104
Vijayalakshmi
  • 189
  • 1
  • 3
  • 11

2 Answers2

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.

Community
  • 1
  • 1
Prakash K
  • 11,669
  • 6
  • 51
  • 109
  • 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. – Vijayalakshmi Jun 25 '12 at 04:48
  • 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 helpful – Prakash K Jun 25 '12 at 04:58
  • 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. – Vijayalakshmi Jun 29 '12 at 04:56
  • @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. – Prakash K Jun 29 '12 at 06:13
  • @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. – Prakash K Jun 29 '12 at 06:14
  • Thank you prakash. I will look into and get back to you after looking into it. – Vijayalakshmi Jul 04 '12 at 09:50
  • 2
    -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 _WAR_ – Alex Punnen May 13 '13 at 10:53
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(); %>
Sebastian Roth
  • 11,344
  • 14
  • 61
  • 110
Konstantin
  • 101
  • 2
  • 6