Monday, May 31, 2010

Render pzn cmpnt using wcm api

How can I render a Personalization (PZN) component using the IBM® Web Content Management (WCM) API?


Answer
The WCM API provides features to render content items as well as library components such as the Web Content Management Personalization component.


Summary of how to render personalization component in WCM

First get the user workspace
Next set the web content library.
Get the document iterator for wcm personalization component by name.
Set the rendering context where the wcm personalization component will be rendered.
Get the wcm personalization component document id
Render the wcm personalization component.


***Note:** The example code (below) renders the WCM Personalization component from a JSP (Java server page).


Example code

//finds component that matches by name
DocumentIdIterator componentIdIterator = workspace.findComponentByName("PZNCMPT");
DocumentId docId;
LibraryComponent component;
String componentName = "";
String libraryPath = "/MyLibrary";
String sitePath = "/site/sitearea";

// Create the rendering context
RenderingContext context = workspace.createRenderingContext(pageContext.getRequest(),pageContext.getResponse(), new java.util.HashMap(), "http://localhost:10038/wps/wcm", "/connect");

// Set the path (Context) to the for the site area where the component will be rendered
context.setRenderedContent(libraryPath + sitePath);

//standard out log message to browser
out.println("Log: Testing WCM API: Context Path = " + context.getPath() + "

");

//loops through the component id's found in the iterator
while(componentIdIterator.hasNext())
{
docId = (DocumentId)componentIdIterator.next();
component = (LibraryComponent) workspace.getById(docId);
componentName= (String)component.getName();

//standard out log message to browser
out.println("Log: Testing WCM API: Component name = " + componentName + "

");

// Get the rendered string
if (component instanceof LibraryComponent)
{
//standard out log message to browser
out.println("Log: Testing WCM API: " + componentName
+ " is an instance of LibraryComponent

");

String renderedContent = workspace.render(context, component);

//standard out log message to browser
out.println("Log: Testing WCM API:: Passed workspace.render(context, component)

");

//Render the PZN Component
out.println("
Render PZN Component: Test Result follows:
" + renderedContent + "
");

}// end if statement

//standard out log message to browser
out.println("The End
");

}//end while