Liferay: get PortletID and companyID from init()

Maybe trough PortletConfig in init(PortletConfig)

The thing is that using

((PortletConfigImpl) portletConfig).getPortletId();

is not allowed anymore because adding portal-impl.jar in gives throws an exception when trying to execute build ant target, saying that this is not allowed anymore

For companyID I directly have no idea where to start. I am using currently

long companyId = CompanyLocalServiceUtil.getCompanies().get(0).getCompanyId();

but as soon as I got more than one company it will fail

If only I could get Portlet object somehow, I think it would be enough to get both portletId and companyId


You can get the Portlet from PortletConfig through the portletId got from the method Portletconfig.getPortletName() and then using PortletLocalServiceUtil.getPortletById(String):

// I have not tried this but I think it should work
String portletName = Portletconfig.getPortletName();
Portlet portlet = PortletLocalServiceUtil.getPortletById(portletName);
long companyId = portlet.getCompanyId();

And then from the Portlet you can easily get the companyId.

A word of caution as stated by Olaf Kock in his answer, hence here is another answer which gives you some alternative to fetch the companyId if you know some aspect of the company. This approach does not fetch the companyId from the portlet instance for the reasons as explained by Olaf Kock.

Try this approach and see if this helps.

I'm not one 100% sure what you need, but if you have just one company, you can use the following code to get the company id:


Adding portal-impl.jar to plugins has always been a source of problems and has never been the correct way to do. With that I'm quite happy to see that it actually seems to be actively prohibited now by the build scripts. As discussed in this question you should not do this and the portlet API does not support what you want. You strictly don't have the context necessary for knowing which company a portlet instance has been added to.

Keep in mind: There might (and typically is) only one instance of your portlet class ever, no matter if a portlet instance is placed on any page - and independent of how many portlet instances are around in the whole portal, across all companies and sites.

You should not follow this path, instead explain what you're trying to achieve with it. We might help you with that, but cannot help you learning how to implement something that is impossible, or, if you find a loophole, will be unmaintainable and randomly break with the next update because it's far outside the specification.

Need Your Help

How do I add PHP files to my android APK file?

php android apk

I have a web application that uses both localstorage but also server side SQL storage. When I test my APK file using Eclipse only the html pages with localstorage work and not the server stuff in t...

jQuery append function shows result in an increase number

jquery append

My goal is that every time i choose an operation and press "save" button, a tag with this operation's name will appear after class change_creation. Here's my code

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.