« 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 添付ファイル
65840 参照数
平均 (3 投票)
平均評価は3.33333333333333星中の5です。
コメント
コメント 作成者 日時
Nice article. Thanks Ramy! Jorge Ferrer 2009/12/09 21: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 0:34
Hi Ramy, This is a very good article for which... Dharmender Singh 2010/04/26 3:48
How do we set Guest's view permission to false... Stan X 2010/09/01 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/07 18:41
I am creating the pages and setting the portlet... Saurabh Gupta 2013/02/28 7:03
please,tell me how add web content... Yogesh Sapkal 2014/02/17 4:13
please,tell me how add web content... Yogesh Sapkal 2014/02/17 4:13
For Liferay 6.2 CE GA4 prefs values are... Nicola Baiocco 2015/07/30 0:32
In Liferay 6.2 CE GA4 we also added: Portlet... Nicola Baiocco 2015/08/20 8:51

投稿日時:09/12/09 21: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
Thomas Ballerstedtへのコメント。投稿日時:09/12/11 0:34
Hi Ramy,

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

Thanks Ramy emoticon
投稿日時:10/04/26 3:48
How do we set Guest's view permission to false on a layout?
投稿日時:10/09/01 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
Mikhail Nikolaenkoへのコメント。投稿日時:10/12/07 18:41
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/02/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/02/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/02/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/07/30 0: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.
Nicola Baioccoへのコメント。投稿日時:15/08/20 8:51