Liferay

This page completely focus on every step of liferay development

Create a Layout, Add portlet Dynamically to 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());



1 comment:

  1. Thanks JAY nice post , I was searching for this thing for a long time.good u saved my time.

    keep posting.

    ReplyDelete