Liferay is a Gartner Magic Quadrant Leader for the Sixth Year! Find out why
« Back to 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 Attachments
55424 Views
Average (3 Votes)
The average rating is 3.33333333333333 stars out of 5.
Comments
Threaded Replies Author Date
Nice article. Thanks Ramy! Jorge Ferrer December 9, 2009 9:30 PM
Hi Ramy, nice christmas gift for me. That's... Thomas Ballerstedt December 10, 2009 9:33 AM
I was thinking to see something really... Peter Fox December 11, 2009 12:34 AM
Hi Ramy, This is a very good article for which... Dharmender Singh April 26, 2010 3:48 AM
How do we set Guest's view permission to false... Stan X September 1, 2010 7:34 AM
Hi Ramy! First I would like to thank you for... Mikhail N November 23, 2010 1:59 AM
Hi Mikhail, On your first issue, I'm not sure... Ramy Georgy December 7, 2010 6:41 PM
I am creating the pages and setting the portlet... Saurabh Gupta February 28, 2013 7:03 AM
please,tell me how add web content... Yogesh Sapkal February 17, 2014 4:13 AM
please,tell me how add web content... Yogesh Sapkal February 17, 2014 4:13 AM
For Liferay 6.2 CE GA4 prefs values are... Nicola Baiocco July 30, 2015 12:32 AM
In Liferay 6.2 CE GA4 we also added: Portlet... Nicola Baiocco August 20, 2015 8:51 AM

Nice article. Thanks Ramy!
Posted on 12/9/09 9:30 PM.
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.
Posted on 12/10/09 9:33 AM.
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
Posted on 12/11/09 12:34 AM in reply to Thomas Ballerstedt.
Hi Ramy,

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

Thanks Ramy emoticon
Posted on 4/26/10 3:48 AM.
How do we set Guest's view permission to false on a layout?
Posted on 9/1/10 7:34 AM.
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
Posted on 11/23/10 1:59 AM.
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
Posted on 12/7/10 6:41 PM in reply to 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
Posted on 2/28/13 7:03 AM.
please,tell me how add web content programmatically in page.
i know from control panel but i want from programmatically......

thanks in advance
Posted on 2/17/14 4:13 AM.
please,tell me how add web content programmatically in page.
i know from control panel but i want from programmatically......

thanks in advance
Posted on 2/17/14 4:13 AM.
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
Posted on 7/30/15 12:32 AM.
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.
Posted on 8/20/15 8:51 AM in reply to Nicola Baiocco.