Forums de discussion

Remove Portlet Dynamically through Java code

thumbnail
Siva K, modifié il y a 9 années.

Remove Portlet Dynamically through Java code

New Member Publications: 20 Date d'inscription: 18/06/13 Publications récentes
I was tried to remove the portlet dynamically but not able do it. The below code is working without issues but portlet is not removing from the page.

String siteOwnerEmail = GetterUtil.get(PropsUtil.get("site.owner"), "test@liferay.com");
liferayCompany = CompanyLocalServiceUtil.getCompanyByWebId("liferay.com");
User siteOwner = UserLocalServiceUtil.getUserByEmailAddress(liferayCompany.getCompanyId(), siteOwnerEmail);
Group siteGroup = GroupLocalServiceUtil.getFriendlyURLGroup(liferayCompany.getCompanyId() , "/abcportalcommunity");
Layout homePageLayout = LayoutLocalServiceUtil.getFriendlyURLLayout(siteGroup.getGroupId() , true, "/home");
LayoutTypePortlet dashboardTypePortlet = (LayoutTypePortlet)homePageLayout.getLayoutType();
String abcPortletId = "Test" + PortletConstants.WAR_SEPARATOR + "pluginportlet";
if (dashboardTypePortlet.hasPortletId(abcPortletId )) {
dashboardTypePortlet.removePortletId(siteOwner.getUserId(), abcPortletId);
LayoutLocalServiceUtil.updateLayout(homePageLayout , true);
}


Note : If user has liferay Administrator role then it is working (portlet is removed from page). if user doesn't have admin role then it is not removing.

And also no error in console.

Please help me on this
thumbnail
Vipin Bardia, modifié il y a 9 années.

RE: Remove Portlet Dynamically through Java code

Regular Member Publications: 162 Date d'inscription: 28/02/11 Publications récentes
Hi Siva,

Please try to use below code to do that -


<a class="taglib-icon" href="/c/portal/update_layout?p_l_id=LAYOUT_ID&amp;p_p_id=PORTET_QUALIFIED_NAME&amp;p_v_l_s_g_id=10197&amp;doAsUserId=&amp;cmd=delete&amp;referer=%2Fc%2Fportal%2Flayout%3Fp_l_id%3D3936301%26doAsUserId%3D&amp;refresh=1" id="_PORTET_QUALIFIED_NAME_INSTANCEID" onclick="Liferay.Portlet.close('#p_p_id_PORTET_QUALIFIED_NAME_'); return false;" target="_self"> 
   <img class="icon" src="www.domain.com/html/themes/classic/images/portlet/close.png" alt="Remove" title="Remove" id="aui_3_4_0_1_1447"> 
</a>


This will remove portlet from screen and update layout without refresh.
To use this snippet user should have permission of updateLayout.

Or to execute your code -

Add below lines above your code -


 Company companyqq = CompanyLocalServiceUtil.getCompanyByWebId("liferay.com");
				 Role adminRole = RoleLocalServiceUtil.getRole(companyqq.getCompanyId(),"Administrator");
				 List<user> adminUsers = UserLocalServiceUtil.getRoleUsers(adminRole.getRoleId());
				 
				 PrincipalThreadLocal.setName(adminUsers.get(0).getUserId());
				 PermissionChecker permissionChecker =PermissionCheckerFactoryUtil.create(adminUsers.get(0));
				 PermissionThreadLocal.setPermissionChecker(permissionChecker);
</user>


After your code initialize permission checker as null.

Regards.
thumbnail
Siva K, modifié il y a 9 années.

RE: Remove Portlet Dynamically through Java code

New Member Publications: 20 Date d'inscription: 18/06/13 Publications récentes
Thanks Vipin Bardia.. Its working now. Thank you.