How to delete a portlet in Liferay 6.1 programmatically from code

烂漫一生 提交于 2019-12-13 20:12:21

问题


I'm working on Liferay 6.1
I want to delete a portlet in Liferay 6.1 from my code. What I have done so far is:

<a onclick="Liferay.Portlet.close('#p_p_id_28_'); return false;">Remove</a>

Above code is working fine. But it is working on the current page only i.e. it can delete the portlet(s) which is there on the current page only.

But I want to delete the portlet(s) which could be some where on the menu of my portal using its layout id.

Please suggest a way out. Thanks in advance.

Regards,
Varun Jain


回答1:


public void removePortlets(ActionRequest request, ActionResponse response)
            throws PortletException {
        ThemeDisplay themeDisplay = (ThemeDisplay) request
                .getAttribute(WebKeys.THEME_DISPLAY);
        long groupId = themeDisplay.getScopeGroupId();
        String friendlyURL = "/demochildpage";
        boolean privateLayout = false;
        long userId = themeDisplay.getUserId();
        try {
            Layout layout = LayoutLocalServiceUtil.getFriendlyURLLayout(
                    groupId, privateLayout, friendlyURL);
            LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout
                    .getLayoutType();
            layoutTypePortlet.removePortletId(userId, "28");
            LayoutLocalServiceUtil.updateLayout(layout.getGroupId(),
                    layout.getPrivateLayout(), layout.getLayoutId(),
                    layout.getTypeSettings());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



回答2:


Way to go would be: query the database for all Layouts on which the portlet is on, than iterate and delete. You would need to implement a custom query or a dynamic query if I am not overseeing something. Maybe there already is a service function to get the desired Layout Id's, but I doubt that.




回答3:


Never tried, but I think, you can achieve what you want using Liferay's service.

The process seems to be simple enough as following:
1. Get list of all pages / layouts using com.liferay.portal.service.LayoutLocalServiceUtil.getLayouts(long groupId, boolean privateLayout)
2. Iterate layout list and extract list of portletIds from layout object casted to com.liferay.portal.model.LayoutTypePortlet
3. Iterate portletIds and compare each porteltId with your portletId string.
4. If matched call com.liferay.portal.model.layoutTypePortlet.removePortletId(long userId, String portletId) and update layout using LayoutLocalServiceUtil.updateLayout(Layout layout)

So, the sample code would look like:

String portletId = "#p_p_id_28_";
long userId = user.getUserId();
ArrayList<Layout> layouts = LayoutLocalServiceUtil.getLayouts(10180, true);

for(Layout layout : layouts){
    if(!layout.isHidden()){
        LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType();
        ArrayList<String> layoutPortletIds = layoutTypePortlet.getPortletIds();

        for(String layoutPortletId : layoutPortletIds){
            if(layoutPortletId.equalsIgnoreCase(portletId)){
                layoutTypePortlet.removePortletId(userId, portletId);
                LayoutLocalServiceUtil.updateLayout(layout);
            }
        }
    }
}


来源:https://stackoverflow.com/questions/30456078/how-to-delete-a-portlet-in-liferay-6-1-programmatically-from-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!