Each more we feel the need of becoming more and more web 2.0, it sounds like a physics law to the "guys" that are trying to survivor arround the amazing applications that surround us across the urls.
Since few months ago I want to blog post about the new Liferay functionality that let you build PortletURLs using JavaScript only.
There are many places you can apply this new way of generating PortletURLs. Imagine a big list of links on your portlet, instead of download repeated hundreds urls from the server site you can simply create a javascript function that retuns a PortletURL instance and set on the fly the parameters you need. And now, you can simply integrate your pure javascript files (.js) with PortletURLs, without over passing it as parameter for your javascript constructor or another work-arround came from our misterious mind.
The usage of this functionality using JavaScript is very simple:
var portletURL = new Liferay.PortletURL();
portletURL.setParameter("key1", "value");
portletURL.setParameter("key2", "value");
portletURL.setPortletId(86);
alert( "that is the url: " + portletURL.toString() );
If you want to know what methods you can play with the JavaScript Liferay.PortletURL you can take a quick look on the methods below:
setCopyCurrentRenderParameters: function(copyCurrentRenderParameters); setDoAsUserId: function(doAsUserId); setEncrypt: function(encrypt); setEscapeXML: function(escapeXML); setLifecycle: function(lifecycle); setName: function(name); setParameter: function(key, value); setPlid: function(plid); setPortletConfiguration: function(portletConfiguration); setPortletId: function(portletId); setPortletMode: function(portletMode); setResourceId: function(resourceId); setSecure: function(secure); setWindowState: function(windowState); toString: function();
I've also created some shortcuts for diferent kind of urls on the portal:
var actionURL = Liferay.PortletURL.createActionURL(); // = new Liferay.PortletURL('ACTION_PHASE');
var renderURL = Liferay.PortletURL.createRenderURL(); // = new Liferay.PortletURL('RENDER_PHASE');
var resourceURL = Liferay.PortletURL.createResourceURL(); // = new Liferay.PortletURL('RESOURCE_PHASE');
var permissionURL = Liferay.PortletURL.createPermissionURL(portletResource, modelResource, modelResourceDescription, resourcePrimKey);
I hope it helps.

6 Comments
Thanks very much for verifying. Do you have any idea when the fix will be released?
The problem i'm having is that when i construct a url with javascript, the url does not comply with one generated with the actionURL tag of the portlet tag lib.
I found these differences in the parameters (TL is tag generated, JL is javascript generated link):
1) a set of underscores too many surrounding the portlet id:
TL: ?p_p_id=Foo_WAR_Bar_INSTANCE_s38I
JL: ?p_p_id=_Foo_WAR_Bar_INSTANCE_s38I_
TL: &_Foo_WAR_Bar_INSTANCE_s38I_javax.portlet.action=something
JL: &__Foo_WAR_Bar_INSTANCE_s38I__javax.portlet.action=something
2) url type parameter is different:
TL:&p_p_url_type=1
JL: &p_p_url_type=0
Is there a method to change this url type maybe?
These difference result in the action not getting called...
It's added by the URL Factory automatically and there is no way to disable this, at least until now. Is there some problem in having this _ on the keys? when you get this parameter on the backend you can retrieve this using the real name, without the _.
Let me know if it helps.
-- Eduardo
Thanks a lot for the convenience of using pure javascript to construct a liferay URL.
I'm running into a problem with it though; is it expected that the keys get prependend with "_[portlet id]_"?
Is there a way to disable this?
Thanks in advance for your time.