Search code examples
javaiframeliferayportletportal

How to add iframes dynamically through code, in liferay


I have a scenario in which users who are created in the liferay will be assigned tools(in the form of iframe). The number of iframes will differ for each user, and when the user logs in, that many number of iframes should come up automatically in his public page.

How can I achieve this ? Is there a way I can save this in preferences of public pages(which will be unique for each user) ? Or should I be using the DB to achieve the same? Thank you


Solution

  • Adding of iframes to liferay can be done dynamically from code, and this is achieved by following code:

                long companyId = themeDisplay.getCompanyId();
            long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
            int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
            Layout layout = LayoutLocalServiceUtil.getLayout(themeDisplay.getPlid());
            LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType();
            String iframePortletId = layoutTypePortlet.addPortletId(themeDisplay.getUserId(),PortletKeys.IFRAME,"column-2",-1);
            PortletPreferences prefs = PortletPreferencesLocalServiceUtil.getPreferences(companyId,ownerId,ownerType,layout.getPlid(),iframePortletId);
            prefs.setValue("src", "http://www.google.com");
            com.liferay.portal.model.PortletPreferences objPortletPref=PortletPreferencesLocalServiceUtil.updatePreferences(ownerId, ownerType,                     layout.getPlid(), iframePortletId, prefs);
            LayoutLocalServiceUtil.updateLayout(layout.getGroupId(),layout.isPrivateLayout(),layout.getLayoutId(),layout.getTypeSettings());    
    

    This will add an iframe dynamically.

    Thank you.