Featured post

Docker setup for Liferay 7 with MySQL

Wednesday 13 February 2013

Add / Edit Web Content through custom portlet

Hi,

May be you have used it earlier but it was never so easy.

In every project we need to add/edit/delete web content from our custom portlet.
Hope it helps...

<!--

<%@page import="com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil"%>
<%@page import="com.liferay.portlet.journal.model.JournalArticle"%>
<%@page import="java.util.List"%>
<%@page import="com.liferay.portal.util.PortalUtil"%>
<%@page import="com.liferay.portal.util.PortletKeys"%>
<%@page import="javax.portlet.WindowState"%>


<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib prefix="liferay-portlet" uri="http://liferay.com/tld/portlet" %>
<%@ taglib prefix="liferay-ui" uri="http://liferay.com/tld/ui" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>

<portlet:defineObjects />
<liferay-theme:defineObjects />
This is the <b>Add Web Content</b> portlet.

// Add Web Content Link

<liferay-portlet:renderURL windowState="<%= WindowState.MAXIMIZED.toString() %>" var="addArticleURL" portletName="<%= PortletKeys.JOURNAL %>">
<portlet:param name="struts_action" value="/journal/edit_article" />
<portlet:param name="redirect" value="<%= PortalUtil.getCurrentURL(renderRequest) %>" />
<portlet:param name="portletResource" value="<%= portletDisplay.getId() %>" />
<portlet:param name="groupId" value="<%= String.valueOf(scopeGroupId) %>" />
</liferay-portlet:renderURL>

<liferay-ui:icon image="add_article" message="add-web-content" url="<%= addArticleURL %>" />

<div>
<%
   List<JournalArticle> listJournalartArticles=JournalArticleLocalServiceUtil.getArticles(scopeGroupId);
   for(JournalArticle journal:listJournalartArticles){
%>

// Edit Web Content Link

<liferay-portlet:renderURL windowState="<%= WindowState.MAXIMIZED.toString() %>" var="editURL" portletName="<%= PortletKeys.JOURNAL %>">
        <portlet:param name="struts_action" value="/journal/edit_article" />
        <portlet:param name="redirect" value="<%= PortalUtil.getCurrentURL(renderRequest) %>" />
        <portlet:param name="originalRedirect" value="<%= PortalUtil.getCurrentURL(renderRequest) %>" />
        <portlet:param name="groupId" value="<%=String.valueOf(scopeGroupId)%>" />
        <portlet:param name="articleId" value="<%=journal.getArticleId()%>" />
        <portlet:param name="version" value="<%=String.valueOf(journal.getVersion())%>" />
</liferay-portlet:renderURL>
<liferay-ui:icon image="edit" message="edit-web-content" url="<%= editURL %>" />

<liferay-ui:journal-article articleId="<%=journal.getArticleId()%>" groupId="<%=scopeGroupId%>" />
<%
    }
%>

</div>

-->

With help of above code you can add/edit web content. It will redirect you on same portlet after adding/editing portlet.

You need to make some changes in above code which i used to display  list of web contents so you can get unique one.

Note: If there is a hyphen(-) in your portlet name then it will through an exception in second deployment.
           Either create portlet without hyphen(-) or follow this link to resolve this issue and have hyphens.

You are just done, Try & Enjoy the function.............:))