« 返回到 Portal Tutorials

Programmatically Create a Layout and Place a Portlet on it

Introduction #

This page will show you how to programmatically:

  1. Create a new layout (page)
  2. Change the layout template for that page
  3. Place a content display portlet with a journal article already selected
  4. 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());
0 附件
65825 查看
平均 (3 票)
满分为 5,平均得分为 3.33333333333333。
评论
讨论主题回复 作者 日期
Nice article. Thanks Ramy! Jorge Ferrer 2009年12月9日 下午9:30
Hi Ramy, nice christmas gift for me. That's... Thomas Ballerstedt 2009年12月10日 上午9:33
I was thinking to see something really... Peter Fox 2009年12月11日 上午12:34
Hi Ramy, This is a very good article for which... Dharmender Singh 2010年4月26日 上午3:48
How do we set Guest's view permission to false... Stan X 2010年9月1日 上午7:34
Hi Ramy! First I would like to thank you for... Mikhail N 2010年11月23日 上午1:59
Hi Mikhail, On your first issue, I'm not sure... Ramy Georgy 2010年12月7日 下午6:41
I am creating the pages and setting the portlet... Saurabh Gupta 2013年2月28日 上午7:03
please,tell me how add web content... Yogesh Sapkal 2014年2月17日 上午4:13
please,tell me how add web content... Yogesh Sapkal 2014年2月17日 上午4:13
For Liferay 6.2 CE GA4 prefs values are... Nicola Baiocco 2015年7月30日 上午12:32
In Liferay 6.2 CE GA4 we also added: Portlet... Nicola Baiocco 2015年8月20日 上午8:51

Nice article. Thanks Ramy!
在 09-12-9 下午9:30 发帖。
Hi Ramy,

nice christmas gift for me. That's what I was looking for. Now I don't have to digg through the sources too much I suppose.!

Thanks Ramy.
在 09-12-10 上午9:33 发帖。
I was thinking to see something really new...but wrong....

Take a look into 7Cogs Hook plugin and you will see that nothing new with this one.
Also you can learn from hook how to add DL documents, update portlet preferences, create layouts... add portlets to public private pages, setup users and roles...

ALL programmatic

BR

Peter
在 09-12-11 上午12:34 发帖以回复 Thomas Ballerstedt
Hi Ramy,

This is a very good article for which i am looking for.

Thanks Ramy emoticon
在 10-4-26 上午3:48 发帖。
How do we set Guest's view permission to false on a layout?
在 10-9-1 上午7:34 发帖。
Hi Ramy!
First I would like to thank you for you article. It is really helpful to me.
But I have some questions/problems.

First issue is after calling this part of code:
String journalPortletId = layoutTypePortlet.addPortletId(userId, PortletKeys.JOURNAL_CONTENT, "column-3", -1);

I am getting id="56" without instance. I have checked sources (6.0.5) and found that liferay checks isInstanceable() property and only if it is true then create instance. In my case JOURNAL_CONTENT is not instanceable. But I expect that this portlet is instanceable. So what I have wrong?

Second question: could you please tell me if is code is mandatory:
LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType();
layoutTypePortlet.setLayoutTemplateId(userId, "my_layout_template_id");
?

I have no custom templates so I do not run this code.

with best regards
Mike
在 10-11-23 上午1:59 发帖。
Hi Mikhail,

On your first issue, I'm not sure why your JOURNAL_CONTENT portlet is not instanceable. The only thing I can think of is if you have overridden its default configuration in liferay-portlet-ext.xml.

As for your second question, no you do not need to change the layout template. It is only if you want to use a different layout template than the default.

Regards,
Ramy
在 10-12-7 下午6:41 发帖以回复 Mikhail Nikolaenko
I am creating the pages and setting the portlet as above mentioned but i have 2 question, please if any body can help me out then it will really good.
1) When add a new page(Layout) programmatically, then how to make this login enable. means when user will click on link of that page, then if he is login can see the page and if he not login then login page will come to him and when he will login then he can see the page.
2) one more question that if owner of that page also can view the page he can not Update
or any thing else.
Thank you in advance
在 13-2-28 上午7:03 发帖。
please,tell me how add web content programmatically in page.
i know from control panel but i want from programmatically......

thanks in advance
在 14-2-17 上午4:13 发帖。
please,tell me how add web content programmatically in page.
i know from control panel but i want from programmatically......

thanks in advance
在 14-2-17 上午4:13 发帖。
For Liferay 6.2 CE GA4 prefs values are different, here an example:

prefs.setValue("articleId", journalArticle.getArticleId());
prefs.setValue("groupId", String.valueOf(groupId));

Hope this helps
在 15-7-30 上午12:32 发帖。
In Liferay 6.2 CE GA4 we also added:
Portlet portlet = PortletLocalServiceUtil.getPortletById(serviceContext.getCompanyId(), journalPortletId);
PortletLayoutListener portletLayoutListener = portlet.getPortletLayoutListenerInstance();
if (portletLayoutListener != null) {
// Results in a persistence call
portletLayoutListener.onAddToLayout(journalPortletId, layout.getPlid());
}

at the end.
Without these lines we found some "issues" during web content display search.
在 15-8-20 上午8:51 发帖以回复 Nicola Baiocco