Friday, April 10, 2009

Change a Portlet Title at "Run Time" WebSphere Portal V6!

To change portlet title at runtime . This following approach suggested by IBM

http://www.ibm.com/developerworks/websphere/library/techarticles/0612_rick/0612_rick.html


After setting the title in RenderResponse.setTitle() method,
you need to have following code to calculate the dynamic portlet id :

public void getDynamicPortletID(RenderRequest request) throws Exception {
String portletId = "";
try {
HttpServletRequest httpRequest = (HttpServletRequest)request;
RunData rundata = RunData.from(httpRequest);
LayoutNode layoutnode =
(LayoutNode) rundata.getAttribute("com.ibm.wps.composition.element");

com.ibm.portal.ObjectID objectid;

if (layoutnode == null)
throw new IllegalStateException("PortletIDTag: Control cannot be found!");
objectid = layoutnode.getObjectID();
if (objectid != null)
portletId = IdentificationMgr.getIdentification().serialize(objectid,false);

}
catch (Exception e) {
e.printStackTrace();

}
}
After calculating the dynamic portletId, with the help of this
portletId use the following code in your
respective JSP


var dynamicTitle = "<%=request.getAttribute(com.ibm.portal.portlet.Constants.DYNAMIC_TITLE)%>";
var titleElement =document.getElementById("title.<%=portletID%>");
if (titleElement != null) {
if(dynamicTitle != "" && dynamicTitle != "null")
titleElement.innerHTML = dynamicTitle;

Note :- This works fine with Mozilla and IE 7.0 browsers.
But has problems with IE 6.0.It gives "Operation TimeOut"
Error.


For reason that why it happeans :-

http://support.microsoft.com/kb/181050

Hence to solve this problem, I used DOJO. As WebSphere Portal Server
implicitly uses DOJO, it was better to find a solution using DOJO

function init() {

var dynamicTitle = "<%=request.getAttribute(com.ibm.portal.portlet.Constants.DYNAMIC_TITLE)%>";

var titleElement =document.getElementById("title.<%=portletID%>");
if (titleElement != null) {
if(dynamicTitle != "" && dynamicTitle != "null")
titleElement.innerHTML = dynamicTitle;
}
}
dojo.addOnLoad( init );

Now it works fine with all the Browzers........