Friday, January 30, 2015

Adding a Page ,Change Layout, Adding a Portlet on a page Programmatically(via Code) in Liferay.

Normally we add a page, change layout adding a portlet on a particular page by using drag and drop or by clicking our mouse.Today we will add a page,Change Layout from 2 to 3 and than finally add a portlet on newly created page Programmatically.

For doing this we basically focus on two classes:-


  • Because page means layout in liferay thats why we use LayoutLocalServiceUtil
  • For using method of LayoutLocalServiceUtil we need userID , groupID so we also need ThemeDisplay Object.

Object of this Class is used to programmatically add or remove a portlets from a page.This object can be get in jsp by <liferay-theme:defineObjects />

Ok Lets discuss step by Step

Step 1 :- Create Liferay project and Generic Portlet in it.

Create a liferay project then create a generic portlet in it . Copy this code inside your view.jsp


Here we create just 3 action URL that hit 3 different methods on click.

Step 2 :- Adding a Page.

On Clicking Adding Page addPage method is called which is responsible for adding a page in Liferay.

Here we use addLayout method of LayoutLocalServiceUtil Class which take parameters as:-

a)UserId:- Current User Id .
b)GroupId:- Group id from which user is belong.
c)Private Layout:- The page added as a public(false) page or private(true) page.
d)name:- Name of the page appears in menu.
e)title:-That is show on tabs of browser.
f)description:- description about the page.
g)type:- type of page .ex- Panel ,embedded etc.
i)hidden:- page is shown in menu bar or not.
j)friendlyUrl:- the friendly Url for the page.

After Clicking the link just Reload the page will appear in menu bar.

Step 3 :-Change Page Layout 

By default the page is added with 2 column .If you want to change from 2 column layout to 3 column layout click on update page Layout link. 

>Here first we get the Layout(ie page) Object for which we have to change the layout by providing friendlyUrl and other values.
>Then we get the LayoutTypePortlet Object and then use setLayoutTempelateId method.
>Other values rather than "3_columns" can be found (\ROOT\layouttpl\custom)

Step 4:- Adding Portlet on Page

Now we have a page with 3 Column layout.Now we add Loan Calculator portlet on the page. Just Click the link Add Loan Calculator this will call the addPortlet method.

>Here first we again get the Layout(ie page) Object on which we have to add the portlet by providing friendlyUrl and other values.
>Then we use the method addPortletId which take 4 parameters as:-
a)UserId:- Current User Id .
b)PortletId:- id of the portlet which is to be added on page.
c)ColoumnId:-In which column you want to add your portlet. Ex- column-1 hence portlet added at LHS if column-1 then portlet added at centre and if column-3 portlet added at RHS.
d)Column Position:- Consider if there is already any other portlet in particular column.
                 0>=:-Our portlet is added at the top of existing portlet.
                -1 :-Our portlet is added at the bottom of existing portlet.

>Finally we update the layout

you can download source code from Here

Hope this will help....

Related Post:-


Total Pageviews

Number Of Unique Visitor

Free counters!