Vishal G
Vishal G

Reputation: 171

Programmatically adding portlets

Is it possible to add a portlet programmatically? If yes, please help me to understand the steps for that.

Upvotes: 2

Views: 2670

Answers (2)

Emanuele Righetto
Emanuele Righetto

Reputation: 720

Something like:

ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
Layout layout = themeDisplay.getLayout();
long plid = layout.getPlid();
long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
long companyId = themeDisplay.getCompanyId();

String portletIdInc = layoutTypePortlet.addPortletId(userId, thisPortletID);

// Retrieve the portlet preferences portlet instance just created
PortletPreferences prefs = PortletPreferencesLocalServiceUtil
    .getPreferences(companyId, ownerId, ownerType, plid, portletIdInc);
// set desired language
String languageId = LanguageUtil.getLanguageId(request);

String urlImage = .... ;
prefs.setValue("portlet-setup-title-" + languageId, report.getName());
prefs.setValue("portlet-setup-use-custom-title", "true");
prefs.setValue("src", report.getUrl());
prefs.setValue("img", urlImage);
prefs.store();
String targetColumn = "column-1";
// update the portlet preferences
PortletPreferencesLocalServiceUtil.updatePreferences(ownerId,
        ownerType, plid, portletIdInc, prefs);

if (Validator.isNotNull(targetColumn) && Validator.isNotNull(portletIdInc)) {
    layoutTypePortlet.movePortletId(userId, portletIdInc, targetColumn, 2);
}

LayoutServiceUtil.updateLayout(layout.getGroupId(), layout.isPrivateLayout(), 
        layout.getLayoutId(), layout.getTypeSettings());

Upvotes: 0

Olaf Kock
Olaf Kock

Reputation: 48057

Liferay CE comes with the "sevencogs-hook" that contains code to set up the demo content, e.g. the "Seven Cogs" virtual company used to demo a Liferay site. This sets up a complete site programmatically. You can read that code and learn how users and pages are created, portlets are added to pages and configured to show what they are supposed to show. More than that: It's running code that can easily be read and is - as side effect - a nice piece of documentation.

http://svn.liferay.com/repos/public/plugins/trunk/hooks/sevencogs-hook/

Login: "guest", no password

or download the source for the version you're referring to.

Upvotes: 1

Related Questions