Friday, July 10, 2009

How to copy content between content libraries using the Web Content Management (WCM) API

You would like to use the IBM® Web Content Management (WCM) API to copy content from one WCM content library to another WCM content library. Which WCM API methods can be used?

Resolving the problem
The WCM API provides methods which allow copying content items between WCM content libraries.
Example algorithm to copy content between WCM libraries

1. First get the user workspace.
2. Next get the source and target document libraries.
3. Set the source document library to be your current document library.
4. Build the site document iterator.
5. Get the site document id.
6. Copy the site using the copyToLibrary method. (note: This method copies non-hierarchical or root items to another library.)
7. Get the new site document id.
8. Build the sitearea document iterator.
9. Get the sitearea document id.
10. Copy the sitearea using the copySiteFrameworkDocument method. (note: This method copies hierarchical items (SiteArea, Content, or ContentLink) to another library.)
11. Get the new sitearea document id.
12. Build the content document iterator.
13. Get the content document id.
14. Copy the content using the copySiteFrameworkDocument method. (note: This method copies hierarchical items (SiteArea, Content, or ContentLink) to another library.)
15. Get the new content document id.


** Note:** This example assumes the content item and associated parents (site/sitearea) do not already exist in target library.

Example code to sites, site areas, and content items between WCM libraries

//define variables
DocumentLibrary sourceDocLib = null;
DocumentLibrary targetDocLib = null;
DocumentLibrary currentDocLib = null;

DocumentIdIterator docIdIterator = null;
DocumentId docId = null;

Site currentSite = null;
SiteArea currentSiteArea = null;
Content currentContent = null;

Document newSiteDoc = null;
DocumentId newSiteDocId = null;

Document newSiteAreaDoc = null;
DocumentId newSiteAreaDocId = null;

Document newContentDoc = null;
DocumentId newContentDocId = null;


//set the content library variables
sourceDocLib = ws.getDocumentLibrary("SupportLib");
targetDocLib = ws.getDocumentLibrary("TargetLib");

//standard out log message
System.out.println("Log: The source document library name: "
+ sourceDocLib.getName());
System.out.println("Log: The target document library name: "
+ targetDocLib.getName());

//set the current content library to the source library
ws.setCurrentDocumentLibrary(sourceDocLib);

//get the current content library
currentDocLib = ws.getCurrentDocumentLibrary();

//standard out log message
System.out.println("Log: The current document library name: "
+ currentDocLib.getName());

/*
**************************************************************************************
COPY SITE FROM SOURCE TO TARGET
**************************************************************************************
*/

//finds the document id of the sites that match by name
docIdIterator = ws.findByName(DocumentTypes.Site, "SupportSite");

//loops through the document id's found in the iterator
while(docIdIterator.hasNext())
{
//get the current document id
docId = (DocumentId)docIdIterator.next();

//get the current site
currentSite = (Site)ws.getById(docId);

//standard out log message
System.out.println("Log: Copy site: "
+ (String)currentSite.getName()
+ " from " + sourceDocLib.getName()
+ " to " + targetDocLib.getName());
/*
*The Workspace copyToLibrary method copies non-hierarchical or
*root items to another library.
*Get the new document id to the new document copy.
*/
newSiteDoc = ws.copyToLibrary(targetDocLib , docId) ;

newSiteDocId = newSiteDoc.getId();

}//end while

/*
**************************************************************************************
COPY SITE AREA FROM SOURCE TO TARGET
**************************************************************************************
*/

//finds the document id of the site areas that match by name
docIdIterator = ws.findByName(DocumentTypes.SiteArea, "Home");

//loops through the document id's found in the iterator
while(docIdIterator.hasNext())
{
//get the current document id
docId = (DocumentId)docIdIterator.next();

//get the current site area
currentSiteArea = (SiteArea)ws.getById(docId);

//standard out log message
System.out.println("Log: Copy site area: "
+ (String)currentSiteArea.getName()
+ " from " + sourceDocLib.getName()
+ " to " + targetDocLib.getName());
/*
*The Workspace copySiteFrameworkDocument method copies hierarchical items
*(SiteArea, Content, or ContentLink) to another library.
*Get the new document id to the new document copy.
*/

newSiteAreaDoc =
ws.copySiteFrameworkDocument(docId, newSiteDocId, null, ChildPosition.END) ;

newSiteAreaDocId = newSiteAreaDoc.getId();

}//end while

/*
**************************************************************************************
COPY CONTENT FROM SOURCE TO TARGET
**************************************************************************************
*/

//finds the document id of the content items that match by name
docIdIterator = ws.findByName(DocumentTypes.Content, "WelcomePage");

//loops through the document id's found in the iterator
while(docIdIterator.hasNext())
{
//get the current document id
docId = (DocumentId)docIdIterator.next();

//get the current content
currentContent = (Content)ws.getById(docId);

//standard out log message
System.out.println("Log: Copy content: "
+ (String)currentContent.getName()
+ " from " + sourceDocLib.getName()
+ " to " + targetDocLib.getName());
/*
*The Workspace copySiteFrameworkDocument method copies hierarchical items
*(SiteArea, Content, or ContentLink) to another library.
*Get the new document id to the new document copy.
*/

newContentDoc =
ws.copySiteFrameworkDocument(docId, newSiteAreaDocId, null, ChildPosition.END) ;

newContentDocId = newContentDoc.getId();

}//end while

Javadoc HTML reference files

WCM api-javadoc: The Javadoc HTML files are located in the following location on your Web Content Management server:

under :
\AppServer\profiles\wp_profile\installedApps\nodename\wcm.ear\ilwwcm.war\webinterface\