This wiki does not contain official documentation and is currently deprecated and read only. Please try reading the documentation on the Liferay Developer Network, the new site dedicated to Liferay documentation. DISCOVER Build your web site, collaborate with your colleagues, manage your content, and more. DEVELOP Build applications that run inside Liferay, extend the features provided out of the box with Liferay's APIs. DISTRIBUTE Let the world know about your app by publishing it in Liferay's marketplace. PARTICIPATE Become a part of Liferay's community, meet other Liferay users, and get involved in the open source project. « Retour - Portal Tutorials
Programmatically Create a Layout and Place a Portlet on it
Introduction #
This page will show you how to programmatically:
- Create a new layout (page)
- Change the layout template for that page
- Place a content display portlet with a journal article already selected
- Save the layout settings
Create a new layout (page) #
Use one of the addLayout methods of the LayoutLocalServiceUtil
long userId = themeDisplay.getUserId();
long groupId = themeDisplay.getScopeGroupId();
boolean privateLayout = false;
long parentLayoutId = 0;
String name = "myNewPage";
String title = null;
String description = null;
String type = LayoutConstants.TYPE_PORTLET;
boolean hidden = true;
String friendlyURL = "/myNewPage";
Layout layout = LayoutLocalServiceUtil.addLayout(userId,
groupId,
privateLayout,
parentLayoutId,
name,
title,
description,
type,
hidden,
friendlyURL);Change the layout template for that page #
LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType(); layoutTypePortlet.setLayoutTemplateId(userId, "my_layout_template_id");
Place a content display portlet with a journal article already selected #
// add a content display portlet
// The column id and position (last 2 parameters below) will depend on your layout template
String journalPortletId = layoutTypePortlet.addPortletId(userId,
PortletKeys.JOURNAL_CONTENT,
"column-3",
-1);
long companyId = themeDisplay.getCompanyId();
long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
// Retrieve the portlet preferences for the journal portlet instance just created
PortletPreferences prefs = PortletPreferencesLocalServiceUtil.getPreferences(companyId,
ownerId,
ownerType,
layout.getPlid(),
journalPortletId);
// set desired article id for content display portlet
prefs.setValue("article-id", "123456");
prefs.setValue("group-id", String.valueOf(groupId));
// update the portlet preferences
PortletPreferencesLocalServiceUtil.updatePreferences(ownerId, ownerType, layout
.getPlid(), journalPortletId, prefs);Save the layout settings #
Now we update the layout to save our changes
LayoutLocalServiceUtil.updateLayout(layout.getGroupId(),
layout.isPrivateLayout(),
layout.getLayoutId(),
layout.getTypeSettings()); 66692 vues