12/02/2010

How to create a link to a WebContent in Liferay

Problem

You want to have a link to a WebContent in Liferay, to send to a colleague or a friend. This friend should then look at the WebContent without having to navigate through Liferay. And : This should be as flexible as possible, without having to create a WebContent Display Portlet for each created WebContent.

Solution

There are actually two solutions:

1 Linking diretly to the WebContent:

If you link diretly to the WebContent, the WebContent is shown without showing the Portal itself. Just as you would link directly to an html page. This is what you need to do to get the link:

1.1

Add portalURL and pathMain together to create a link to the Server Liferay is running on :



String pathToWebContent = themeDisplay.getPortalURL() +  themeDisplay.getPathMain();

2.1

Add the Path to directly view a WebContent:
pathToWebContent += "/journal/view_article_content?groupId="
3.1

Add GroupId, ArticleId and Version of the WebContent you want to display :
pathToWebContent += journalArticle.getGroupId() + "&articleId=" + journalArticle.getArticleId() + "&version=" +journalArticle.getVersion();
This creates something like the following which you can send by email:


http://localhost:8080/c/journal/view_article_content?groupId=12041&articleId=73245&version=1.0

2 Linking to an asset publisher


If you want to display the WebContent IN the portal, so that the User afterwards can start to explore your portal (maybe you want to lure some customers onto your website) you can do the following :

To summarize it : We deploy an Asset Publisher Portlet to the portal, search this Asset Publisher in our code and pass the Web Content data to it.

2.1 Prepare the portal

At first, create a new site in your portal, that will only take an asset publisher. Then deploy an asset publisher somewhere on that page. This asset publisher will now be the place where all your web contents will be displayed. Enter the Configuration of the asset publisher and change "asset selection" to "manual". That´s all for the asset publisher.
The last thing you have to do is to remember the friendly url that leads to your asset publisher. Look at the status bar of your browser : http://localhost:8080/user/admin/test. In this case it would be "test".

2.2 Navigate to the asset publisher

What your method needs is the friendly url ("test") and the journalArticle (= Web Content) to navigate to the asset publisher. So how do we navigate there ?

First, retrieve all Layouts and inspect the friendlyURL property if it equals the friendly URL you passed into the method:
List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(QueryUtil.ALL_POS, QueryUtil.ALL_POS); 
[...]
 if (friendlyURL.equals("/" + friendlyUrl)) ...
What you then need to do is to retrieve the UnicodeProperties containing the properties you need to inspect for an asset publisher ID. The asset publisher ID is 101 :
UnicodeProperties typeSettingsProperties = layout.getTypeSettingsProperties();
String property= typeSettingsProperties.getProperty("column-2");

If you have this property, you need the instance ID of the asset publisher to build the link correctly:
 String assetId= property.subString(property.indexOf("_INSTANCE_") + 10);
And the last step is to combine all that to the link :
String url = getUserPage() + "/" + friendlyUrl + "/-/asset_publisher/" + assetId+ "/content/" + journalArticle.getUrlTitle();

Thats all :) Now you can see the Web Content in your Portal by clicking on a link.


If you like this tutorial it would be very nice, if you could click on some of the google ads you see on the right side. It helps me run this block and motivates me ;)

If you have any questions, feel free to leave a comment.