Programmatically get the url of a page in liferay

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).


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


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.