Programmatically get the url of a page in liferay
Asked Answered
H

4

18

I want to create a link in a portlet so that I can navigate to a different page in the liferay portal. I order to do that I am looking for an API (can be liferay specific) that given a page name, would return it's url (it can be the friendlyURL as well).

Halfhour answered 27/1, 2011 at 13:6 Comment(0)
C
8

The API to access pages in Liferay is the LayoutService. However, page names are not unique in Liferay and furthermore they are internationalized. So you need some unique property for a page to retrieve its url, besides its name.

If you really only have the page name, you can use LayoutLocalServiceUtil.getLayouts(...) to loop over all Layouts and check for some property (in this case its name).

Corneille answered 1/3, 2011 at 15:10 Comment(1)
FriendlyURL isn't unique AFAIK. Two pages (Layouts) in different Groups can have same friendly URL. Actually they will, because when you create new Group you usually create page with "/home" friendly URL in it. The thing is that friendly URL of a page (Layout) doesn't contain complete path (like "/web/guest/home", "/web/user/home") but only a part of it.Burchett
B
12

Portal pages in Liferay are indeed called Layouts in APIs and DB tables. They're identified by plid field, can be obtained using LayoutLocalServiceUtil and related APIs, and also from some other calls like themeDisplay.getLayout().

However in order to build String containing URL to a page you'll have to concatenate friendly URL of portal, group and layout itself (i.e. /web/guest/home - web is portal URL for public pages, guest is friendly URL of guest group by default and home is friendly URL of home page/layout by default). This can be tricky, as you have to check whether this is a public or private page, etc.

And once you start using virtual hosts with friendly URLs for groups, things change. So this is not a good way.

To avoid manually creating URLs and have URLs that are guaranteed to be correct you should use com.liferay.portal.util.PortalUtil.getLayoutFriendlyURL(Layout layout, ThemeDisplay themeDisplay) a static method of PortalUtil - it'll do all the necessary work for you. Though you also need to provide ThemeDisplay and not only Layout.

Burchett answered 15/2, 2012 at 12:36 Comment(0)
C
8

The API to access pages in Liferay is the LayoutService. However, page names are not unique in Liferay and furthermore they are internationalized. So you need some unique property for a page to retrieve its url, besides its name.

If you really only have the page name, you can use LayoutLocalServiceUtil.getLayouts(...) to loop over all Layouts and check for some property (in this case its name).

Corneille answered 1/3, 2011 at 15:10 Comment(1)
FriendlyURL isn't unique AFAIK. Two pages (Layouts) in different Groups can have same friendly URL. Actually they will, because when you create new Group you usually create page with "/home" friendly URL in it. The thing is that friendly URL of a page (Layout) doesn't contain complete path (like "/web/guest/home", "/web/user/home") but only a part of it.Burchett
B
1

At least in Liferay 6.1.20 one can use 'Link Portlet URLs to Page' to make links proceed to antoher page in your portal. It is under ´look and feel´ menu item of your portlet. For me this works fine.

cheers

Bambino answered 21/7, 2014 at 13:27 Comment(0)
N
1

try this one for current url

${themeDisplay.getURLCurrent()}

try this one for portal url

${themeDisplay.getPortalURL()}
Niveous answered 29/5, 2020 at 13:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.