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

Vijayalakshmi picture Vijayalakshmi · Jun 22, 2012 · Viewed 18k times · Source

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

Answer

Prakash K picture Prakash K · Jun 23, 2012

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.