How programmatically to add a portlet into Layouts (pages)

Some times you need to add portlet using java-code (e.g. during importing a publication from the old portal).  Let’s look how can we do it with WebContentDisplay.

In my case I have used stand-alone program so I had to define this portlet:

private void initPortlet() {
    String[] xmls = new String[] {
    // xml from /WEB-INF/portlet.xml
    “<?xml version=”1.0″?>” +
    “<portlet-app>” +
       “<portlet>” +
           “<portlet-name>56</portlet-name>” +
           “<display-name>Journal Content</display-name>” +
       “</portlet>” +
    “</portlet-app>”
    , null
    // xml from /WEB-INF/liferay-portlet.xml
    , “<?xml version=”1.0″?>” +
    “<liferay-portlet-app>” +
       “<portlet>” +
            “<portlet-name>56</portlet-name>” +
           “<display-name>Journal Content</display-name>” +
           “<instanceable>true</instanceable>” +
       “</portlet>” +
    “</liferay-portlet-app>”
    , null
    , null
    };
    // define the portlet (note: such code invokes from MainServlet.java only)
    ServletContext sc = new org.apache.catalina.core
                                                       .ApplicationContext(“”, new StandardContext());
    PortletLocalServiceUtil.initEAR(sc, xmls, null);
}

Now we can use next code:

    long companyId, groupId, userId; // define properties as you need
    long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
    int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
    // get public layout by id
    Layout layout = LayoutLocalServiceUtil.getLayout(groupId, false, id);
    LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType();
    // set column where portlet will be added
    String template = layoutTypePortlet.getLayoutTemplateId();
    String columnId;
    if (“two-columns-layout”.equals(template)) {
        columnId = “column-2”;
    } else {
       // other conditions
    }
    // add portlet (returns new id)
    String journalPortletId = layoutTypePortlet.addPortletId(userId
                                                            , PortletKeys.JOURNAL_CONTENT, columnId, -1);
    // update layout (important)
    LayoutLocalServiceUtil.updateLayout(layout.getGroupId(), layout.isPrivateLayout(),
                                                                                    layout.getLayoutId(), layout.getTypeSettings());
    // retrieve the portlet preferences for the journal portlet instance just created
    PortletPreferences prefs = PortletPreferencesLocalServiceUtil.getPreferences(companyId,
                                                                                ownerId, ownerType, layout.getPlid(), journalPortletId);
    // set article id for content display portlet
    prefs.setValue(“article-id”, article.getArticleId());
    prefs.setValue(“group-id”, String.valueOf(groupId));
    // update the portlet preferences
    PortletPreferencesLocalServiceUtil.updatePreferences(ownerId, ownerType
                                                                                                    , layout.getPlid(), journalPortletId, prefs);

Note: this portlet can appear after few minutes only. So if you don’t want to wait you should clean the database cache.

Enjoy the new portlet with article in it!