Sunteți pe pagina 1din 44

Implementing common extension scenarios using IBM Lotus

Web Content Management 6.1 APIs

Bhargavkumar Thakker
Staff Software Engineer
IBM Software Group
Pune, India

Soumitra Limaye
Staff Software Engineer
IBM Software Group
Pune, India

October 2009

© Copyright International Business Machines Corporation 2009. All rights reserved.

Abstract: The IBM® Lotus® Web Content Management™ (hereafter called “Web Content
Management”) API provides an extension of standard features of Web Content Management. This
article provides usage and solutions with code samples of the most common implementations
carried out by customers using APIs. You can pick and choose the samples provided here, as is, to
incorporate into your system.

Contents
1 Introduction..................................................................................................................................... 2
2 Assumptions and considerations..................................................................................................... 4
3 Code samples to implement various scenarios using APIs per the requirement ............................ 4
Scenario 1: Find an item of a given type by name......................................................................... 4
Scenario 2: Find items based on the item type .............................................................................. 8
Scenario 3: Find content by various criteria ................................................................................ 12
Scenario 4: Search for content based on Authoring Template, Site Area, Category, and
Keywords criteria......................................................................................................................... 16
Scenario 5: Creating a new Site ................................................................................................... 18
Scenario 6: Creating a new Site Area .......................................................................................... 19
Scenario 7: Creating new Content ............................................................................................... 21
Scenario 8: Creating a new Content Link that is linked to existing Content............................... 23
Scenario 9: Creating various Library Components..................................................................... 24

-1-
Scenario 10: Creating a new Taxonomy and a new Category ..................................................... 25
Scenario 11: Creating and canceling drafts on published Content items..................................... 26
Scenario 12: Moving content to Next Stage of a Workflow, Restarting the Workflow, setting
another Workflow on specific content......................................................................................... 28
Scenario 13: Render Sites, Site Areas, Contents, Content components, or Library Components
...................................................................................................................................................... 29
Scenario 14: Moving or copying an entire Site Area within/between libraries........................... 31
Scenario 15: Moving or copying content within/between libraries ............................................. 33
Scenario 16: Moving or copying non-hierarchical or root items to another library .................... 35
Scenario 17: Moving or copying a Category within/between libraries ....................................... 39
Scenario 18: Deleting and purging Objects ................................................................................. 41
4 Standards for names specified in all examples ............................................................................. 43
5 Conclusion .................................................................................................................................... 43
6 Resources ...................................................................................................................................... 44
7 About the authors.......................................................................................................................... 44

1 Introduction
You can extend the standard features of Web Content Management using the product’s API and
JavaServer Pages™ (JSP) files. The Lotus Web Content Management API is focused on
processing the content programmatically and is an alternative way to access a Web Content
Management site. It allows content and other Web Content Management objects to be created,
edited, deleted, and rendered entirely by using Java™ code.

Additionally, you can always manipulate your site and integrate Web Content Management with
other applications in a way that was not previously possible using these APIs.

There are many functions that can be achieved using Web Content Management APIs; here are a
few examples:

• Web content libraries:


o Moving or copying items within a library.
o Moving or copying items between libraries.

• Search iterators of item IDs:


o Find item of given type by name.
o Find items of given type.
o Find library components by name.
o Find content by authoring template.
o Find content by category.
o Find content by path.
o Find content by workflow stage.
o Find content modified since date.
o Find content modified between dates.

-2-
• Content search (similar to the Menu Component search criteria but using item parameters
as ids).

• Retrieve via item IDs.

• The ability to create, delete and save the following items:


o Content
o Sites
o Site Areas
o File Resource components
o HTML components
o Image components
o Date and Time components
o Link components
o Number components
o Rich text components
o Style Sheet components
o Short text components
o Text components
o User Selection components
o taxonomies
o categories

• The ability to retrieve the following items:


o Content
o Sites
o Site areas
o Taxonomies and categories
o Workflows
o Components

• The ability to retrieve the following items from searches, (but NOT as an item):

o Authoring Template ID (Authoring Template)


o Presentation Template ID (Presentation Template)
o Workflow Stage ID

• The ability to approve or reject content items in a workflow stage. Other item-types do not
support this function.

The Javadocs should be reviewed for a complete set of the features available using the API. The
Javadoc HTML files are located under the
was_profile_root\installedApps\nodename\wcm.ear\ilwwcm.war\webinterface\ folder.

All the samples provided below can be picked directly and used or incorporated into your code, to
achieve the required result based on the various scenarios you must carry out in your
environment. We have added comments (in italic font) on most of the lines, to make it easier to
understand the code and flow.

NOTE: For further information, refer to the IBM Lotus Web Content Management API information
center topic, from which the material in this Introduction section is excerpted.

-3-
2 Assumptions and considerations
Before beginning, note the following:

• The user must have access to all the objects mentioned in the code samples below.

• We are not using the try-catch methods to pick up those exceptions.

• The Javadoc for moveToLibrary states "This method moves non-hierarchical or root items to
another library".

• Site and taxonomy are considered to be root items.

• Refer to Section 4, “Standards for names specified in all examples,” in this document for an
explanation of the standards used in the code samples.

3 Code samples to implement various scenarios using APIs


per the requirement
All the below scenarios have been tested on the 6.1 version of Web Content Management;
however, the same logic can be used for version 6.0 with a change in APIs wherever required.

Scenario 1: Find an item of a given type by name


To search for a specific type of an item by name, you can select and use the appropriate API
code in listing 1. With this you can search a Site, Site Area, Content, Content Link, Authoring
template, Presentation template, all library components, Workflow, Workflow stage, Taxonomy,
Category, etc., using the workspace.findbyName API.

This essentially returns an iterator of IDs of all objects of the given type with the specified name.

Listing 1. Example code to find item by name


<%@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="java.util.*"%>

<%
// retrieve repository
Repository repository = WCM_API.getRepository();

// get the workspace for current user


Workspace workspace = repository.getWorkspace(request.getUserPrincipal());

// set the library


workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("WebContent"));

// **************************** Find Site example ***************************

// define site name

-4-
String siteName = new String("Site");

// find site by name


DocumentIdIterator siteIterator = workspace.findByName(DocumentTypes.Site,siteName);

if (siteIterator.hasNext()){
out.println("Site found : " + siteName + "<br>");
}else {
out.println("Could not find site : " + siteName + "<br>");
}

// ************************* Find Sitearea example **************************

// define sitearea name


String siteArea = new String("SiteArea");

// find sitearea by name


DocumentIdIterator siteAreaIterator =
workspace.findByName(DocumentTypes.SiteArea,siteArea);

if (siteAreaIterator.hasNext()){
out.println("Sitearea found : " + siteArea + "<br>");
}else {
out.println("Could not find Sitearea : " + siteArea + "<br>");
}

// ************************* Find Content example ***************************

// define content name


String contentName = new String("Content");

// find content by name


DocumentIdIterator contentIterator =
workspace.findByName(DocumentTypes.Content,contentName);

if (contentIterator.hasNext()){
out.println("Content found : " + contentName + "<br>");
}else {
out.println("Could not find Content : " + contentName + "<br>");
}

// *********************** Find Authoring Template example *******************

// define authoring template name


String authoringTemplateName = new String("AuthoringTemplate");

-5-
// find authoring template by name
DocumentIdIterator authoringTemplateIterator =
workspace.findByName(DocumentTypes.AuthoringTemplate,authoringTemplateName);

if (authoringTemplateIterator.hasNext()){
out.println("Authoring template found : " + authoringTemplateName + "<br>");
}else {
out.println("Could not find Authoring template: " + authoringTemplateName + "<br>");
}

// *********************** Find Presentation Template example *****************

// define presentation template name


String presentationTemplateName = new String("PresentationTemplate");

// find presentation template by name


DocumentIdIterator presentationTemplateIterator =
workspace.findByName(DocumentTypes.PresentationTemplate,presentationTemplateName);

if (presentationTemplateIterator.hasNext()){
out.println("Presentation template found : " + presentationTemplateName + "<br>");
}else {
out.println("Could not find Presentation template: " + presentationTemplateName + "<br>");
}

// *********************** Find Library Component example *******************

// define library component name


String libraryComponentName = new String("LibraryComponent");

// find library component by name


DocumentIdIterator libraryComponentIterator =
workspace.findByName(DocumentTypes.LibraryComponent,libraryComponentName);

if (libraryComponentIterator.hasNext()){
out.println("Library component found : " + libraryComponentName + "<br>");
}else {
out.println("Could not find Library component: " + libraryComponentName + "<br>");
}

// *************************** Find Workflow example ***********************

// define workflow name


String workflowName = new String("Workflow");

// find workflow by name

-6-
DocumentIdIterator workflowIterator =
workspace.findByName(DocumentTypes.Workflow,workflowName);

if (workflowIterator.hasNext()){
out.println("Workflow found : " + workflowName + "<br>");
}else {
out.println("Could not find Workflow: " + workflowName + "<br>");
}

// ************************* Find Workflow Stage example ********************

// define workflow stage name


String workflowStageName = new String("WorkflowStageName");

// find workflow stage by name


DocumentIdIterator workflowStageIterator =
workspace.findByName(DocumentTypes.WorkflowStage,workflowStageName);

if (workflowStageIterator.hasNext()){
out.println("Workflow Stage found : " + workflowStageName + "<br>");
}else {
out.println("Could not find Workflow Stage: " + workflowStageName + "<br>");
}

// ************************** Find Taxonomy example ************************

// define taxonomy name


String taxonomyName = new String("Taxonomy");

// find taxonomy by name


DocumentIdIterator taxonomyIterator =
workspace.findByName(DocumentTypes.Taxonomy,taxonomyName);

if (taxonomyIterator.hasNext()){
out.println("Taxonomy found : " + taxonomyName + "<br>");
}else {
out.println("Could not find Taxonomy: " + taxonomyName + "<br>");
}

// ************************** Find Category example *************************

// define category name


String categoryName = new String("Category");

// find category by name

-7-
DocumentIdIterator categoryIterator =
workspace.findByName(DocumentTypes.Category,categoryName);

if (categoryIterator.hasNext()){
out.println("Category Found : " + categoryName + "<br>");
}else {
out.println("Could not find Category : " + categoryName + "<br>");
}

%>

Scenario 2: Find items based on the item type


To find items based on the specific type, you can directly select and use the appropriate API code
in listing 2. With this you can search all Sites, Site Areas, Contents, Content Links, Authoring
templates, Presentation templates, all library components, Workflows, Workflow stages,
Taxonomies, Categories, etc., using the workspace.findbyType API.

This returns an iterator of IDs of all objects of the given type.

Listing 2. Example code to find items by type


<%@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="java.util.*"%>

<%

// retrieve repository
Repository repository = WCM_API.getRepository();

// get workspace for current user


Workspace workspace = repository.getWorkspace(request.getUserPrincipal());

// set library
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("WebContent"));

// **************************** Find Site example ***************************

// find all sites


DocumentIdIterator siteIterator = workspace.findByType(DocumentTypes.Site);
DocumentId siteId = null;

if(siteIterator.hasNext()){
while(siteIterator.hasNext())
{
siteId = siteIterator.nextId();
out.println("Found Site : " + siteId.getName() + "<br>");
}

-8-
} else {
out.println("Could not find any Site");
}

// ************************** Find Sitearea example *************************

// find all siteareas


DocumentIdIterator siteAreaIterator = workspace.findByType(DocumentTypes.SiteArea);
DocumentId siteAreaId = null;

if(siteAreaIterator.hasNext()){
while(siteAreaIterator.hasNext())
{
siteAreaId = siteAreaIterator.nextId();
out.println("Found Sitearea : " + siteAreaId.getName() + "<br>");
}
} else {
out.println("Could not find any Sitearea");
}

// ************************** Find Content example **************************

// find all contents


DocumentIdIterator contentIterator = workspace.findByType(DocumentTypes.Content);
DocumentId contentId = null;

if(contentIterator.hasNext()){
while(contentIterator.hasNext())
{
contentId = contentIterator.nextId();
out.println("Found Content : " + contentId.getName() + "<br>");
}
} else {
out.println("Could not find any Content");
}

// ********************** Find Authoring Template example *********************

// find all authoring templates


DocumentIdIterator authoringTemplateIterator =
workspace.findByType(DocumentTypes.AuthoringTemplate);

DocumentId authoringTemplateId = null;

if(authoringTemplateIterator.hasNext()){
while(authoringTemplateIterator.hasNext())

-9-
{
authoringTemplateId = authoringTemplateIterator.nextId();
out.println("Found Authoring Template : " + authoringTemplateId.getName() +
"<br>");
}
} else {
out.println("Could not find any Authoring Template");
}

// ********************* Find Presentation Template example ********************

// find all presentation templates


DocumentIdIterator presentationTemplateIterator =
workspace.findByType(DocumentTypes.PresentationTemplate);
DocumentId presentationTemplateId = null;

if(presentationTemplateIterator.hasNext()){
while(presentationTemplateIterator.hasNext())
{
presentationTemplateId = presentationTemplateIterator.nextId();
out.println("Found Presentation Template : " + presentationTemplateId.getName()
+ "<br>");
}
} else {
out.println("Could not find any Presentation Template");
}

// ********************** Find Library Component example *********************

// find all library components


DocumentIdIterator libraryComponentIterator =
workspace.findByType(DocumentTypes.LibraryComponent);
DocumentId libraryComponentId = null;

if(libraryComponentIterator.hasNext()){
while(libraryComponentIterator.hasNext())
{
libraryComponentId = libraryComponentIterator.nextId();
out.println("Found Library component : " + libraryComponentId.getName() +
"<br>");
}
} else {
out.println("Could not find any Library component");
}

// ************************** Find Workflow example *************************

- 10 -
// find all workflows
DocumentIdIterator workflowIterator = workspace.findByType(DocumentTypes.Workflow);
DocumentId workflowId = null;

if(workflowIterator.hasNext()){
while(workflowIterator.hasNext())
{
workflowId = workflowIterator.nextId();
out.println("Found Workflow : " + workflowId.getName() + "<br>");
}
} else {
out.println("Could not find any Workflow");
}

// ************************ Find Workflow Stage example **********************

// find all workflow stages


DocumentIdIterator workflowStageIterator =
workspace.findByType(DocumentTypes.WorkflowStage);
DocumentId workflowStageId = null;

if(workflowStageIterator.hasNext()){
while(workflowStageIterator.hasNext())
{
workflowStageId = workflowStageIterator.nextId();
out.println("Found Workflow Stage : " + workflowStageId.getName() + "<br>");
}
} else {
out.println("Could not find any Workflow Stage");
}

// ************************** Find Taxonomy example ************************

// find all taxonomies


DocumentIdIterator taxonomyIterator = workspace.findByType(DocumentTypes.Taxonomy);
DocumentId taxonomyId = null;

if(taxonomyIterator.hasNext()){
while(taxonomyIterator.hasNext())
{
taxonomyId = taxonomyIterator.nextId();
out.println("Found Taxonomy : " + taxonomyId.getName() + "<br>");
}
} else {
out.println("Could not find any Taxonomy");

- 11 -
}

// ************************** Find Category example *************************

// find all categories


DocumentIdIterator categoryIterator = workspace.findByType(DocumentTypes.Category);
DocumentId categoryId = null;

if(categoryIterator.hasNext()){
while(categoryIterator.hasNext())
{
categoryId = categoryIterator.nextId();
out.println("Found Category : " + categoryId.getName() + "<br>");
}
} else {
out.println("Could not find any Category");
}

%>

Scenario 3: Find content by various criteria


To find content by Authoring Template, Category, path, Workflow Stage, modified since date, and
modified between dates, you can select and use the appropriate API code in listing 3, which uses
the following APIs:

workspace.findContentByAuthoringTemplate
workspace.findContentByCategory
workspace.findContentByPath
workspace.findContentByWorkflowStage
workspace.findContentModifiedSince
workspace.findContentModifiedBetween

Listing 3. Example code to find content by criteria


<%@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="java.util.*"%>

<%

// retrieve repository
Repository repository = WCM_API.getRepository();

// get workspace for current user


Workspace workspace = repository.getWorkspace(request.getUserPrincipal());

// Set library
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("WebContent"));

- 12 -
DocumentIdIterator contentIterator = null;
DocumentId contentId = null;

// **************** Find Content by authoring template example ******************

// define authoring template


String authoringTemplateName = new String("AuthoringTemplate");

// find authoring template by name


DocumentIdIterator authoringTemplateIterator =
workspace.findByName(DocumentTypes.AuthoringTemplate,authoringTemplateName);

DocumentId authoringTemplateId = null;

if (authoringTemplateIterator.hasNext()){

out.println("Authoring Template found : " + authoringTemplateName + "<br>");


authoringTemplateId = authoringTemplateIterator.nextId();

// find all the contents having above authoring template


contentIterator = workspace.findContentByAuthoringTemplate(authoringTemplateId);

if(contentIterator.hasNext()){
contentId = contentIterator.nextId();
out.println("List of Content uses " + authoringTemplateName );
while(contentId!=null) {
out.println( "<BR>" + contentId.getName());
contentId = contentIterator.nextId();
}
} else {
out.println("Could not find any Content that uses " + authoringTemplateName );
}

}else {
out.println("Could not find Authoring Template: " + authoringTemplateName + "<br>");
}

// ******************* Find Content by Category example ***********************

// define category
String categoryName = new String("Category");
DocumentId categoryId = null;

// find category by name


DocumentIdIterator categoryIterator =
workspace.findByName(DocumentTypes.Category,categoryName);

- 13 -
if (categoryIterator.hasNext()){
out.println("Category found : " + categoryName + "<br>");
categoryId = categoryIterator.nextId();

// find all the content having above category


contentIterator = workspace.findContentByCategory(categoryId);

if(contentIterator.hasNext()){
out.println("List of Content uses " + categoryName );
while(contentIterator.hasNext()) {
contentId = contentIterator.nextId();
out.println( "<BR>" + contentId.getName());
}

} else {
out.println("Could not find any Content that uses " + categoryName );
}

}else {
out.println("Could not find Category: " + categoryName + "<br>");
}

// ******************* Find Content by Path example **************************

// define content path


String contentPath = "/Web content/Site/SiteArea/Content";

// find content by path


contentIterator = workspace.findContentByPath(contentPath);

if (contentIterator.hasNext()){
contentId = contentIterator.nextId();
out.println("Content found on Path : " + contentPath + " : Content name : " +
contentId.getName() + "<br>");
}else {
out.println("Could not find Content on Path : " + contentPath + " <BR>");
}

// ******************* Find Content by Workflow Stage example ******************

// define workflow stage


String workflowStageName = new String("WorkflowStageName");

// find workflow stage by name

- 14 -
DocumentIdIterator workflowStageIterator =
workspace.findByName(DocumentTypes.WorkflowStage,workflowStageName);

if (workflowStageIterator.hasNext()){

DocumentId workflowStageId = workflowStageIterator.nextId();


out.println("Workflow Stage found : " + workflowStageName + "<br>");

// find all the content having above workflow stage


contentIterator = workspace.findContentByWorkflowStage(workflowStageId);

if(contentIterator.hasNext()){
contentId = contentIterator.nextId();
out.println("List of contents under Workflow Stage " + workflowStageName );
while(contentId!=null) {
out.println( "<BR>" + contentId.getName());
contentId = contentIterator.nextId();
}

} else {
out.println("Could not find any content under Workflow Stage " +
workflowStageName );
}

}else {
out.println("Could not find Workflow Stage: " + workflowStageName + "<br>");
}

// ******************* Find Content since modified date example *****************

// define start date in dd/mm/yyyy format


Date startDate = new Date("01/01/2008");

// find content modified since start date


contentIterator = workspace.findContentModifiedSince(startDate);

if(contentIterator.hasNext()){

contentId = contentIterator.nextId();
out.println("List of contents modified since " + startDate );
while(contentId!=null) {
out.println( "<BR>" + contentId.getName());
contentId = contentIterator.nextId();
}

} else {

- 15 -
out.println("Could not find any content modified since " + startDate );
}

%>

// **************** Find Content modified between two dates example *************

// define start date in dd/mm/yyyy format


startDate = new Date("01/01/2008");

// define end date in dd/mm/yyyy format


Date endDate = new Date("12/31/2009");

// find all the contents modified between start date and end date
contentIterator = workspace.findContentModifiedBetween(startDate,endDate);

if(contentIterator.hasNext()){
contentId = contentIterator.nextId();
out.println("List of contents modified between " + startDate + "and" + endDate );
while(contentId!=null) {
out.println( "<BR>" + contentId.getName());
contentId = contentIterator.nextId();
}

} else {
out.println("Could not find any content modified between " + startDate + "and" +
endDate );
}

%>

Scenario 4: Search for content based on Authoring Template, Site Area,


Category, and Keywords criteria
To search for content based on criteria that are a combination of Authoring Template, Site Area,
Category, or Keywords, you can select and use the appropriate API code in listing 4. You can do
this by using the ContentSearch method, which returns an iterator of IDs of objects that match
the given search criteria.

The ContentSearch method behaves in a manner similar to the Menu Component. If Site Areas
have been specified, then all the ancestor and descendant Site Areas are also included in the
search.

This performs an "or" search, not an "and" search. This means that a search for two different
Categories and a Content Template will return content profiled with at least one of each profile
type (one Category and one Template), not just content that matches all the parameters. Content
that matches only one criteria type (Template only) will not be returned.

- 16 -
Note that the order of results is not guaranteed, and all parameters are optional except
matchAllKeys, the last parameter.

Listing 4. Example code to search for content by criteria


<%@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="java.util.*"%>

<%
// retrieve repository
Repository repository = WCM_API.getRepository();

// get workspace for current user


Workspace workspace = repository.getWorkspace(request.getUserPrincipal());

// Set library
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("WebContent"));

DocumentId contentId = null;


DocumentId authoringTemplateId = null;
DocumentId siteAreaId = null;
DocumentId categoryId = null;

// define authoring template


String authoringTemplateName = new String("AuthoringTemplate");

// find authoring template by name

DocumentIdIterator authoringTemplateIterator =
workspace.findByName(DocumentTypes.AuthoringTemplate,authoringTemplateName);

if (authoringTemplateIterator.hasNext()){
authoringTemplateId = authoringTemplateIterator.nextId();
out.println("Authoring Template found : " + authoringTemplateName + "<br>");
}else {
out.println("Could not find Authoring Template: " + authoringTemplateName + "<br>");
}

// define siteare
String siteArea = new String("SiteArea");

// find sitearea by name

DocumentIdIterator siteAreaIterator =
workspace.findByName(DocumentTypes.SiteArea,siteArea);

if (siteAreaIterator.hasNext()){

- 17 -
siteAreaId = siteAreaIterator.nextId();
out.println("Sitearea Found : " + siteArea + "<br>");
}else {
out.println("Could not find Sitearea : " + siteArea + "<br>");
}

// define category
String categoryName = new String("Category");

// find category by name


DocumentIdIterator categoryIterator =
workspace.findByName(DocumentTypes.Category,categoryName);

if (categoryIterator.hasNext()){
categoryId = categoryIterator.nextId();
out.println("Category Found : " + categoryName + "<br>");
}else {
out.println("Could not find Category : " + categoryName + "<br>");
}

// define keywords
String[] keywords = new String[]{"Keyword"};

// search all the contents based on authoring template , sitearea , category and keywords

DocumentIdIterator contentIterator = workspace.contentSearch(authoringTemplateId, new


DocumentId [] {siteAreaId}, new DocumentId [] {categoryId}, keywords,true);

if(contentIterator.hasNext()){
contentId = contentIterator.nextId();
while(contentId!=null)
{
out.println("Found Content : " + contentId.getName() + "<br>");
contentId = contentIterator.nextId();
}

} else {
out.println("Could not find any Content using the contentSearch Method");
}

%>

Scenario 5: Creating a new Site


You can create a new site using Web Content Management API methods, after which you can
also use all the methods under Site Object once the site has been created. You can select and

- 18 -
use the appropriate code provided in listing 5, which demonstrates only the creation of a new site,
setting its name, title, description, and then saving it to the repository.

We use workspace.createSite to achieve this, which returns the new Site object.

Listing 5. Example code to create a new Site


<%@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="java.util.*"%>

<%

// retrieve repository
Repository repository = WCM_API.getRepository();

// get workspace for current user


Workspace workspace = repository.getWorkspace(request.getUserPrincipal());

// Set library
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("WebContent"));

// create new site


Site newSite = workspace.createSite();

newSite.setName("NewSite");

newSite.setTitle("NewSite");

newSite.setDescription("New Site Created using WCM API");

String[] saveMessage = workspace.save((Document)newSite);

if (saveMessage.length==0){
out.println("Could not create new Site..<BR>");
}else{
out.println("New Site created successfully.");
}

%>

Scenario 6: Creating a new Site Area


You can also create a new Site Area by using Web Content Management API methods, after
which you can use all the methods under the Site Area Object.

Listing 6 demonstrates only creating a new Site Area, setting its name, title, description, and
saving it to the repository. We use workspace.createSiteArea here, which creates a new Site

- 19 -
Area under the given parent. The parent can be a Site or a Site Area, and the method returns the
new Site Area object.

Listing 6. Example code to create new Site Area


<%@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="java.util.*"%>
<%

// retrieve repository
Repository repository = WCM_API.getRepository();

// get workspace for current user


Workspace workspace = repository.getWorkspace(request.getUserPrincipal());

// Set library
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("WebContent"));

DocumentId documentId = null;


Site parentSite = null;
String parentSiteName = "Site";
DocumentId siblingId = null;

// find parent site


DocumentIdIterator siteIterator = workspace.findByName(DocumentTypes.Site,parentSiteName);
if (siteIterator.hasNext()){
documentId = siteIterator.nextId();
parentSite = (Site) workspace.getById(documentId);

// create new sitearea

SiteArea newSiteArea =
workspace.createSiteArea((DocumentId)parentSite.getId(),siblingId,ChildPosition.END);

newSiteArea.setName("NewSiteArea");
newSiteArea.setTitle("NewSiteArea");
newSiteArea.setDescription("New Sitearea Created using WCM API

String[] saveMessage = workspace.save((Document)newSiteArea);

if (saveMessage.length==0) {
out.println (" <BR> Created new Sitearea under " + parentSiteName );
}

} else {
out.println (" <BR> Could not find parent Site " + parentSiteName + ". Could not create a
new Sitearea." );

- 20 -
}

%>

Scenario 7: Creating new Content


You can create new Content under a specific Site Area at a specific position, using a Web
Content Management API. You can select and use the appropriate provided code in listing 7,
which demonstrates the creation of Content under a specific Site Area at the last position, with a
specific Authoring Template.

The code also configures the Workflow while saving the content. We use
workspace.createContent for this purpose, which returns a new content object based on the
specified Authoring Template. The API code can be changed based on your requirements.

Listing 7. Example code to create a new Content


<%@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="java.util.*"%>

<%

// retrieve repository
Repository repository = WCM_API.getRepository();

// get workspace for current user


Workspace workspace = repository.getWorkspace(request.getUserPrincipal());

// Set library
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("WebContent"));

// define authoring template


String authoringTemplateName = new String("AuthoringTemplate");

// define parent sitearea


String parentSiteAreaName = new String("SiteArea");

// define workflow
String workflowName = new String("Workflow");

DocumentId authoringTemplateId = null;


DocumentId parentSiteAreaId = null;
DocumentId siblingId = null;
DocumentId workflowId = null;

// find authoring template by name


DocumentIdIterator authoringTemplateIterator =
workspace.findByName(DocumentTypes.AuthoringTemplate,authoringTemplateName);

- 21 -
if (authoringTemplateIterator.hasNext()){
authoringTemplateId = authoringTemplateIterator.nextId();
out.println("Authoring Template found : " + authoringTemplateName + "<br>");
}else {
out.println("Could not find Authoring Template: " + authoringTemplateName + "<br>");
}

// find sitearea by name


DocumentIdIterator siteAreaIterator =
workspace.findByName(DocumentTypes.SiteArea,parentSiteAreaName );

if (siteAreaIterator.hasNext()){
parentSiteAreaId = siteAreaIterator.nextId();
out.println("Sitearea Found : " + parentSiteAreaName + "<br>");
}else {
out.println("Could not find Sitearea : " + parentSiteAreaName + "<br>");
}

// find workflow by name


DocumentIdIterator workflowIterator =
workspace.findByName(DocumentTypes.Workflow,workflowName);

if (workflowIterator.hasNext()){
workflowId = workflowIterator.nextId();
out.println("Workflow found : " + workflowName + "<br>");
}else {
out.println("Could not find Workflow: " + workflowName + "<br>");
}

if((authoringTemplateId!=null) && (parentSiteAreaId!=null) && (workflowId!=null)){

// create new content


Content newContent =
workspace.createContent(authoringTemplateId,parentSiteAreaId,siblingId,ChildPosition.END);
newContent.setName("NewContent");
newContent.setTitle("NewContent");
newContent.setDescription("New Content created using WCM API");
newContent.setWorkflowId(workflowId);

String[] saveMessage = workspace.save((Document)newContent);

if (saveMessage.length==0) {
out.println (" <BR> Created new Content under " + parentSiteAreaName );
}

- 22 -
}else {
out.println ("Could not create new content");
}

%>

Scenario 8: Creating a new Content Link that is linked to existing


Content
You can create a new Content Link that is linked to existing Content under a specific Site Area.
The Content must be saved before creating a link to it, while the Content Link is created
immediately, and should not be saved.

We use workspace.createContentLink to achieve this, which returns a new Content Link object.
The code in listing 8 assumes that the Content Link is not present under the Site Area.

Listing 8. Example code to create a new Content Link


<%@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="java.util.*"%>

<%
// retrieve repository
Repository repository = WCM_API.getRepository();

// get workspace for current user


Workspace workspace = repository.getWorkspace(request.getUserPrincipal());

// Set library
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("WebContent"));

// define content
String contentName = new String("Content");

// define sitearea where the content link needs to be created


String parentSiteArea = new String("NewSiteArea");

DocumentId contentId = null;


DocumentId parentSiteAreaId =null;
DocumentId siblingId = null;

// find content by name


DocumentIdIterator contentIterator =
workspace.findByName(DocumentTypes.Content,contentName);

if (contentIterator.hasNext())
contentId = contentIterator.nextId();

- 23 -
// find sitearea by name
DocumentIdIterator parentSiteAreaIterator =
workspace.findByName(DocumentTypes.SiteArea,parentSiteArea);

if (parentSiteAreaIterator.hasNext())
parentSiteAreaId = parentSiteAreaIterator.nextId();

if((contentId!=null) && (parentSiteAreaId!=null)) {

// create content link


ContentLink contentLink =
workspace.createContentLink(contentId,parentSiteAreaId,siblingId,ChildPosition.END);
out.println("New Content link of Content " + contentName + " is created under new
Sitearea " + parentSiteArea);
} else{
out.println("Could not create Content link of Content " + contentName + " under new
Sitearea " + parentSiteArea);
}

%>

Scenario 9: Creating various Library Components


You can create Library Components such as DateComponent, DocumentManagerComponent,
FileComponent, HTMLComponent, ImageComponent, JSPComponent, LinkComponent,
RichTextComponent, ShortTextComponent, TextComponent, UserSelectionComponent.

The code in listing 9 demonstrates only creation, setting the name, title, description, and then
saving them in the Web Content Management repository. The code can be directly used to create
a File Component, Image Component, and Rich Text Component:

workspace.createFileComponent
workspace.createImageComponent
workspace.createRichTextComponent

Other APIs can be used once the components are created.

Listing 9. Example code to create various library components


<%@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="java.util.*"%>

<%
// retrieve repository
Repository repository = WCM_API.getRepository();

// get workspace for current user


Workspace workspace = repository.getWorkspace(request.getUserPrincipal());

- 24 -
// Set library
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("WebContent"));

String[] saveMessage = new String[1];

// ***************** Create File Resource Component example ********************

LibraryFileComponent libraryFileComponent = workspace.createFileComponent();


libraryFileComponent.setName("NewFileComponent");
libraryFileComponent.setTitle("NewFileComponent");
libraryFileComponent.setDescription("New File resource component created using API");
saveMessage = workspace.save((Document)libraryFileComponent);
out.println ("<BR> New File resource component created :" + libraryFileComponent.getName());

// ******************* Create Image Component example ***********************

LibraryImageComponent libraryImageComponent = workspace.createImageComponent();


libraryImageComponent.setName("NewImageComponent");
libraryImageComponent.setTitle("NewImageComponent");
libraryImageComponent.setDescription("New Image component created using API");
saveMessage = workspace.save((Document)libraryImageComponent);
out.println ("<BR> New Image component created :" + libraryImageComponent.getName());

// ******************* Create Rich Text Component example **********************

LibraryRichTextComponent libraryRichTextComponent =
workspace.createRichTextComponent();
libraryRichTextComponent.setName("NewRichTextComponent");
libraryRichTextComponent.setTitle("NewRichTextComponent");
libraryRichTextComponent.setDescription("New Rich text component created using API");
saveMessage = workspace.save((Document)libraryRichTextComponent);
out.println ("<BR> New Rich text component created :" +
libraryRichTextComponent.getName());

%>

Scenario 10: Creating a new Taxonomy and a new Category


You can create a new Taxonomy as well as a new Category under a specific Category or
Taxonomy. The code in listing 10 demonstrates creating a new Taxonomy and new Category
under a specific parent Taxonomy, and you can use this code directly in your code. We use:

workspace.createTaxonomy, which returns the new Taxonomy object


workspace.createCategory, which returns the new Category object

- 25 -
Listing 10. Example code to create a new Taxonomy and new Category
<%@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="java.util.*"%>

<%
// retrieve repository
Repository repository = WCM_API.getRepository();

// get workspace for current user


Workspace workspace = repository.getWorkspace(request.getUserPrincipal());

// Set library
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("WebContent"));

String[] saveMessage = new String[1];

// *********************** Create Taxonomy example **************************

Taxonomy parentTaxonomy = workspace.createTaxonomy();


parentTaxonomy.setName("NewParentTaxonomy");
parentTaxonomy.setTitle("NewParentTaxonomy");
parentTaxonomy.setDescription("New Taxonomy created using WCM API");
saveMessage = workspace.save((Document)parentTaxonomy);
if (saveMessage.length==0) {
out.println (" <BR> Created new Taxonomy : " + parentTaxonomy.getName());
}

// *********************** Create Category example **************************

Category newCategory = workspace.createCategory((DocumentId)parentTaxonomy.getId());


newCategory.setName("NewCategory");
newCategory.setTitle("NewCategory");
newCategory.setDescription("New Category Created using WCM API");
saveMessage = workspace.save((Document)newCategory);
if (saveMessage.length==0) {
out.println (" <BR> Created new Category : " + newCategory.getName());
}

%>

Scenario 11: Creating and canceling drafts on published Content items


You can create as well as cancel drafts on published content, using a Web Content Management
API. The code in listing 11 can be used directly to create a draft of existing content and also to
cancel an existing draft on published content.

- 26 -
Listing 11. Example code to create and cancel drafts
<%@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="java.util.*"%>

<%
// retrieve repository
Repository repository = WCM_API.getRepository();

// get workspace for current user


Workspace workspace = repository.getWorkspace(request.getUserPrincipal());

// Set library
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("WebContent"));

// define the publish content


String contentName = new String("PublishedContent");
DocumentId contentId = null;

// find content by name


DocumentIdIterator contentIterator =
workspace.findByName(DocumentTypes.Content,contentName);

if (contentIterator.hasNext()){
contentId = contentIterator.nextId();
Content content = (Content) workspace.getById(contentId);

// create draft from publish content


Document draftContent = content.createDraftDocument();
out.println("Draft created for content " + contentName);

// cancel draft
if (content. hasDraft()) {
draftContent = content.cancelDraftDocument();
out.println("Draft cancled for " + contentName);
} else {
out.println("Draft does not exist for content : " + contentName);
}
}

%>

- 27 -
Scenario 12: Moving content to Next Stage of a Workflow, Restarting
the Workflow, setting another Workflow on specific content
You can move content to the next Workflow stage, restart the Workflow, and set another workflow
on specific content. You can use the code in listing 12 to move content from the draft stage to the
publish stage, restart the workflow, and to set another Workflow on a specific content item.

Listing 12. Example code to move Content


<%@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="java.util.*"%>

<%

// retrieve repository
Repository repository = WCM_API.getRepository();

// get workspace for current user


Workspace workspace = repository.getWorkspace(request.getUserPrincipal());

// Set library
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("WebContent"));

// define the draft content


String contentName = new String("Content");
Content content = null;
DocumentId contentId = null;

// define new workflow to be set on content


String newWorkflowName = new String("Workflow");
DocumentId newWorkflowId = null;

// find content by name


DocumentIdIterator contentIterator =
workspace.findByName(DocumentTypes.Content,contentName);

if (contentIterator.hasNext()){
contentId = contentIterator.nextId();
content = (Content) workspace.getById(contentId);

// below code may not be required if the next workflow stage does not exist

// moving content to next workflow stage


content.nextWorkflowStage();
out.println("Draft content" + contentName + "is moved to next workflow stage");

// restarting workflow
content.restartWorkflow();

- 28 -
out.println("<BR> Workflow restarted for content " + contentName);
}

// find new workflow by name


DocumentIdIterator workflowIterator =
workspace.findByName(DocumentTypes.Workflow,newWorkflowName);

if (workflowIterator.hasNext())
newWorkflowId = workflowIterator.nextId();

if((contentId!=null) && (newWorkflowId !=null)) {

// set new \ another workflow on content


content.setWorkflowId(newWorkflowId);

String[] saveMessage = workspace.save((Document)content);


out.println("<BR> New Workflow set on content : " + contentName);
} else {
out.println("<BR> Could not set new Workflow on content : " + contentName);
}

%>

Scenario 13: Render Sites, Site Areas, Contents, Content components,


or Library Components
You can render Sites, Site Areas, Contents, Content components, or Library components. The
given Web Content Management content component is rendered using the specified rendering
context.

Note that it’s not enough to specify only the ContentComponent to be rendered; the path to the
item containing the ContentComponent must also be specified in the RenderingContext.

The code in listing 13 demonstrates rendering Content, Content component, and the Library
component. You can always add or remove more components per your requirements.

Listing 13. Example code to render various artifacts


<%@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="java.util.*"%>

<%
// retrieve repository
Repository repository = WCM_API.getRepository();

// get workspace for current user


Workspace workspace = repository.getWorkspace(request.getUserPrincipal());

- 29 -
// Set library
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("WebContent"));

// ******************* Render Site\ Sitearea or Content example ******************

// define path of the site , sitearea or content to be rendered.

String contentPath = new String ("/WebContent/Site/SiteArea/Content");


Map parametermap = new HashMap();
parametermap.put("srv", "page");

// create rendering context


RenderingContext renderingContext = workspace.createRenderingContext(request, response,
parametermap);

if(renderingContext!=null) {
renderingContext.setRenderedContent(contentPath);
out.println("Rendering content " + contentPath );
out.println ("<BR>" + workspace.render(renderingContext));
} else {
out.println("Could not render content");
}

// ********************* Render Content Component example ********************

// define content name


String contentName = new String("Content");

// define siteArea name


String siteAreaName = new String("SiteArea");

Content content = null;


ContentComponent component=null;
SiteArea siteArea = null;

// find content by name


DocumentIdIterator contentIterator =
workspace.findByName(DocumentTypes.Content,contentName);

if (contentIterator.hasNext()){
content = (Content) workspace.getById(contentIterator.nextId());
component = content.getComponent("MyComponent");
}

// find sitearea by name

- 30 -
DocumentIdIterator siteAreaIterator =
workspace.findByName(DocumentTypes.SiteArea,siteAreaName);

if (siteAreaIterator.hasNext())
siteArea = (SiteArea) workspace.getById(siteAreaIterator.nextId());

if((content!=null) && (siteArea!=null)){


renderingContext.setRenderedContent(content,siteArea);
out.println("<BR> Rendering content component : " + component.getName());
out.println ("<BR>" + workspace.render(renderingContext,component));
} else {
out.println("Could not render Content component");
}

// ********************* Render Library Component example ********************

// define library component


String libraryComponentName = new String("LibraryComponent");

// find library component by name

DocumentIdIterator libraryComponentIterator =
workspace.findByName(DocumentTypes.LibraryComponent,libraryComponentName);

if (libraryComponentIterator.hasNext()){
LibraryComponent libraryComponent = (LibraryComponent)
workspace.getById(libraryComponentIterator.nextId());
renderingContext.setRenderedContent(contentPath);
out.println("<BR> Rendering library component " + libraryComponentName );
out.println ("<BR>" + workspace.render(renderingContext,libraryComponent));
} else {
out.println("<BR> Could not render Library component" + libraryComponentName);
}

%>

Scenario 14: Moving or copying an entire Site Area within/between


libraries
To move or copy an entire Site Area with all its children to another parent site within the same
library or to another library, you can use the API code in listing 14. To move the Site Area, we use
the workspace.moveSiteFrameworkDocument method.

To copy the Site Area, you can use the workspace.copySiteFrameworkDocument method to
replace the workspace.moveSiteFrameworkDocument method, using the same code below.

- 31 -
Listing 14. Example code to move or copy Site Area
<%@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="java.util.*"%>

<%
// retrieve repository
Repository repository = WCM_API.getRepository();

// get workspace for current user


Workspace workspace = repository.getWorkspace(request.getUserPrincipal());

// define sitearea to be move


String siteArea = new String("SiteArea");

// define new parent site


String newParentSite = new String("Site");
DocumentId siteAreaId = null;

// define sibling id
DocumentId siblingId = null;.
DocumentId newParentsiteId = null;

// set source library


workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("Source Library"));

// find sitearea to be moved


DocumentIdIterator siteAreaIterator =
workspace.findByName(DocumentTypes.SiteArea,siteArea);

if (siteAreaIterator.hasNext()){
siteAreaId = siteAreaIterator.nextId();
out.println("Sitearea Found : " + siteArea + "<br>");
}else {
out.println("Could not find Sitearea : " + siteArea + "<br>");
}

// target library. Not required while moving Sitearea to the same library
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("Target Library"));

// find new parent site


DocumentIdIterator siteIterator = workspace.findByName(DocumentTypes.Site,newParentSite);

if (siteIterator.hasNext()){
newParentsiteId = siteIterator.nextId();
out.println("New parent site found : " + newParentSite + "<br>");

- 32 -
}else {
out.println("Could not find new parent site : " + newParentSite + "<br>");
}

if ((newParentsiteId!=null) && (siteAreaId!=null)) {

out.println(" Moving Sitearea <b> " + siteArea + "</b> to new parent site <b>" +
newParentSite + "</b><br>");

// move sitearea to new parent site

workspace.moveSiteFrameworkDocument(siteAreaId,newParentsiteId,siblingId,ChildPosit
ion.START);
out.println(" Moved Sitearea <b>" + siteArea + " </b> to new parent site <b>" +
newParentSite + "</b><br>");

}else{
out.println(" Moving Sitearea <b> " + siteArea + "</b> to new parent site <b>" +
newParentSite + "</b> failed <br>");
}

%>

Scenario 15: Moving or copying content within/between libraries


You can move or copy the content to another Site Area either within the same library or to a
different library, using the code in listing 15. To move the content, we use the
workspace.moveSiteFrameworkDocument method.

To copy the Site Area, you can use the workspace.copySiteFrameworkDocument method to
replace the workspace.moveSiteFrameworkDocument method, using the same code below.

Listing 15. Example code to move or copy content


<%@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="java.util.*"%>

<%
// retrieve repository
Repository repository = WCM_API.getRepository();

// get workspace for current user


Workspace workspace = repository.getWorkspace(request.getUserPrincipal());

// define content to be move


String contentName = new String("Content");

// define new parent siteArea

- 33 -
String newParentSitearea = new String("SiteArea");

// sibling id
DocumentId siblingId = null;
DocumentId contentId = null;
DocumentId newParentsiteareaId = null;

// set source library


workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("Source Library"));

// find content to be moved


DocumentIdIterator contentIterator =
workspace.findByName(DocumentTypes.Content,contentName);

if (contentIterator.hasNext()){
contentId = contentIterator.nextId();
out.println("Content Found : " + contentName + "<br>");
}else {
out.println("Could not find Content : " + contentName + "<br>");
}

// set target library. Not required when moving content to the same library
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("Target Library"));

// find new parent sitearea


DocumentIdIterator newParentSiteareaIterator =
workspace.findByName(DocumentTypes.SiteArea,newParentSitearea);

if (newParentSiteareaIterator.hasNext()){
newParentsiteareaId = newParentSiteareaIterator.nextId();
out.println("New parent Sitearea found : " + newParentSitearea + "<br>");

}else {
out.println("Could not find new parent Sitearea : " + newParentSitearea + "<br>");
}

if ((contentId!=null) && (newParentsiteareaId!=null)) {

out.println(" Moving content <b> " + contentName + "</b> to new parent Sitearea <b>" +
newParentSitearea + "</b><br>");

// move content to new sitearea


workspace.moveSiteFrameworkDocument(contentId,newParentsiteareaId,siblingId,ChildP
osition.START);

- 34 -
out.println(" Moved content <b>" + contentName + " </b> to new parent Sitearea <b>" +
newParentSitearea + "</b><br>");

}else{
out.println(" Moving content <b> " + contentName + "</b> to new parent Sitearea <b>" +
newParentSitearea + "</b> failed.<br>");
}

%>

Scenario 16: Moving or copying non-hierarchical or root items to


another library
To move or copy any non-hierarchical items (Authoring Templates, Presentation Templates,
Library Components, Workflows, Workflow Stages), or root items (Sites and Taxonomies) to
another library, use the code in listing 16. To move the item to another library, we use the
workspace.moveToLibrary method, which moves non-hierarchical or root items to another
library.

To copy the item, we use the workspace.copyToLibrary method to replace the


workspace.moveToLibrary method, using the same code below.

The code in listing 16 shows how we can achieve this for Authoring Templates, Presentation
Templates, Library components, Workflows, Workflow Stages, Sites, and Taxonomies.

Listing 16. Example code to move or copy root items to another library
<%@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="java.util.*"%>

<%

// retrieve repository
Repository repository = WCM_API.getRepository();

// get workspace for current user


Workspace workspace = repository.getWorkspace(request.getUserPrincipal());

// set source library


workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("Source Library"));

// get target library


DocumentLibrary targetDocumentLibrary = workspace.getDocumentLibrary("Target Library");

// ********************** Move Authoring Template example ******************

// define authoring template


String authoringTemplateName = new String("AuthoringTemplate");

- 35 -
// find authoring template by name
DocumentIdIterator authoringTemplateIterator =
workspace.findByName(DocumentTypes.AuthoringTemplate,authoringTemplateName);

if (authoringTemplateIterator.hasNext()){
DocumentId authoringTemplateId = authoringTemplateIterator.nextId();
out.println("Authoring Template found : " + authoringTemplateName + "<br>");
out.println(" Moving Authoring Template <b> " + authoringTemplateName + "</b> to new
library <b> Target Library </b><br>");

// move authoring template to another library


workspace.moveToLibrary(targetDocumentLibrary ,authoringTemplateId );
out.println(" Moved Authoring Template <b>" + authoringTemplateName + " </b> to another
library <b> Target Library </b><br>");

}else {
out.println("Could not find Authoring Template: " + authoringTemplateName + "<br>");
}

// ********************** Move Presentation Template example ******************


// define presentation template
String presentationTemplateName = new String("PresentationTemplate");

// find presentation template by name


DocumentIdIterator presentationTemplateIterator =
workspace.findByName(DocumentTypes.PresentationTemplate,presentationTemplateName);

if (presentationTemplateIterator.hasNext()){
DocumentId presentationTemplateId = presentationTemplateIterator.nextId();
out.println("Presentation Template found : " + presentationTemplateName + "<br>");
out.println(" Moving Presentation Template <b> " + presentationTemplateName + "</b> to
new library <b> Target Library </b><br>");

// move presentation template to another library


workspace.moveToLibrary(targetDocumentLibrary ,presentationTemplateId ); // Moving
presentation tempate to another library
out.println(" Moved Presentation Template <b>" + presentationTemplateName + " </b> to
another library <b> Target Library </b><br>");

}else {
out.println("Could not find Presentation Template: " + presentationTemplateName + "<br>");
}

// ********************** Move Library Component example *********************

- 36 -
// define library component
String libraryComponentName = new String("LibraryComponent");

// find library component by name


DocumentIdIterator libraryComponentIterator =
workspace.findByName(DocumentTypes.LibraryComponent,libraryComponentName);

if (libraryComponentIterator.hasNext()){
DocumentId libraryComponentId = libraryComponentIterator.nextId();
out.println("Library Component found : " + libraryComponentName + "<br>");
out.println(" Moving Library Component <b> " + libraryComponentName + "</b> to new
library <b> Target Library </b><br>");

// move library component to new library


workspace.moveToLibrary(targetDocumentLibrary ,libraryComponentId );
out.println(" Moved Library Component <b>" + libraryComponentName + " </b> to
another library <b> Target Library </b><br>");

}else {
out.println("Could not find Library Component: " + libraryComponentName + "<br>");
}

// ************************* Move Workflow example *************************

// define workflow
String workflowName = new String("Workflow");

// find workflow by name


DocumentIdIterator workflowIterator =
workspace.findByName(DocumentTypes.Workflow,workflowName);

if (workflowIterator.hasNext()){
DocumentId workflowId = workflowIterator.nextId();
out.println("Workflow found : " + workflowName + "<br>");
out.println(" Moving Workflow <b> " + workflowName + "</b> to new library <b> Target
Library </b><br>");

// move workflow to another library


workspace.moveToLibrary(targetDocumentLibrary ,workflowId );
out.println(" Moved workflow <b>" + workflowName + " </b> to another library <b>
Target Library </b><br>");

}else {
out.println("Could not find Workflow: " + workflowName + "<br>");
}

- 37 -
// ************************ Move Site example ******************************

// define site to be move


String siteName = new String("Site");

// find site by name


DocumentIdIterator siteIterator = workspace.findByName(DocumentTypes.Site,siteName);
DocumentId siteId = null;

if (siteIterator.hasNext()){
out.println("Site found : " + siteName + "<br>");
out.println(" Moving Site <b> " + siteName + "</b> to new library <b> Target Library
</b><br>");
siteId = siteIterator.nextId();

// move site to another library


workspace.moveToLibrary(targetDocumentLibrary ,siteId );
out.println(" Moved Site <b>" + siteName + " </b> to another library <b> Target Library
</b><br>");
}else {
out.println("Could not find Site : " + siteName + "<br>");
}

// ********************** Move Workflow Stage example ***********************

// define workflow stage


String workflowStageName = new String("WorkflowStageName");

// find workflow stage by name


DocumentIdIterator workflowStageIterator =
workspace.findByName(DocumentTypes.WorkflowStage,workflowStageName);

if (workflowStageIterator.hasNext()){
DocumentId workflowStageId = workflowStageIterator.nextId();
out.println("Workflow Stage found : " + workflowStageName + "<br>");
out.println(" Moving Workflow Stage<b> " + workflowStageName + "</b> to new library
<b> Target Library </b><br>");

// move workflowstage to another library


workspace.moveToLibrary(targetDocumentLibrary ,workflowStageId );
out.println(" Moved Workflow Stage<b>" + workflowStageName + " </b> to another
library <b> Target Library </b><br>");

}else {
out.println("Could not find Workflow Stage: " + workflowStageName + "<br>");

- 38 -
}

// ************************ Move Taxonomy example *************************

// define taxonomy
String taxonomyName = new String("Taxonomy");

// find taxonomy by name


DocumentIdIterator taxonomyIterator =
workspace.findByName(DocumentTypes.Taxonomy,taxonomyName);

if (taxonomyIterator.hasNext()){
DocumentId taxonomyId = taxonomyIterator.nextId();
out.println("Taxonomy found : " + taxonomyName + "<br>");
out.println(" Moving Taxonomy <b> " + taxonomyName + "</b> to new library <b>
Target Library </b><br>");

// move taxonomy to another library


workspace.moveToLibrary(targetDocumentLibrary ,taxonomyId );
out.println(" Moved Taxonomy <b>" + taxonomyName + " </b> to another library <b>
Target Library </b><br>");

}else {
out.println("Could not find Taxonomy: " + taxonomyName + "<br>");
}

%>

Scenario 17: Moving or copying a Category within/between libraries


To move or copy a Category in the same library or between libraries, use the code in lisitng 17
below. To move the item in the same or to another library, we use the workspace.moveCategory
method, which essentially moves a category and adds it under a new parent.

To copy the category, we use the workspace.copyCategory method to replace the


workspace.moveToLibrary method, using the same code below.

Note that the example below moves a category to another taxonomy; you can also move the
category to another parent category.

Listing 17. Example code to move or copy a Category


<%@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="java.util.*"%>

<%
// retrieve repository
Repository repository = WCM_API.getRepository();

- 39 -
// get workspace for current user
Workspace workspace = repository.getWorkspace(request.getUserPrincipal());

// Set library
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("WebContent"));

// define category to be move


String categoryName = new String("Category");

// define parent taxonomy


String newTaxonomy = new String("Taxonomy");

DocumentId categoryId = null;


DocumentId taxonomyId = null;

// find category to be moved

DocumentIdIterator categoryIterator =
workspace.findByName(DocumentTypes.Category,categoryName);

if (categoryIterator.hasNext()){
categoryId = categoryIterator.nextId();
out.println("Category Found : " + categoryName + "<br>");
}else {
out.println("Could not find Category : " + categoryName + "<br>");
}

// find taxonomy by name


DocumentIdIterator taxonomyIterator =
workspace.findByName(DocumentTypes.Taxonomy,newTaxonomy);

if (taxonomyIterator.hasNext()){
taxonomyId = taxonomyIterator.nextId();
out.println("Taxonomy found : " + newTaxonomy + "<br>");
}else {
out.println("Could not find Taxonomy: " + newTaxonomy + "<br>");
}

if ((categoryId!=null) && (taxonomyId!=null)) {

out.println(" Moving Category <b> " + categoryName + "</b> to new parent taxonomy
<b>" + newTaxonomy + "</b><br>");

// move category

- 40 -
workspace.moveCategory(categoryId,taxonomyId);
out.println(" Moved Category <b>" + categoryName + " </b> to new parent taxonomy <b>"
+ newTaxonomy + "</b><br>");

}else{
out.println(" Moving Category <b> " + categoryName + "</b> to new parent taxonomy
<b>" + newTaxonomy + "</b> failed <br>");
}

%>

Scenario 18: Deleting and purging Objects


We can delete and purge all types of objects, using Web Content Management APIs. It’s best to
clear all the references of the object being deleted before deleting the actual object; otherwise, the
deletion will fail.

We can also purge (permanently delete) the object from the repository. The code in listing 18
demonstrates deleting as well as purging a content item and as well as a Library Component.

To delete a Site / Site Area / Taxonomy, you must delete all the children prior to deleting the Sites
/ Site Areas / and Taxonomies.

Listing 18. Example code to delete and purge Objects


<%@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="java.util.*"%>

<%
// retrieve repository
Repository repository = WCM_API.getRepository();

// get workspace for current user


Workspace workspace = repository.getWorkspace(request.getUserPrincipal());

// Set library
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("WebContent"));

// ********************** Delete \ Purge Content example **********************

// define content
String contentName = new String("Content");
String[] deleteMessage = new String[1];

// find content by name


DocumentIdIterator contentIterator =
workspace.findByName(DocumentTypes.Content,contentName);
DocumentId contentId = null;

- 41 -
if (contentIterator.hasNext()){
contentId = contentIterator.nextId();

// clear all the references of the content


if(workspace.getReferences(contentId)!=null)
workspace.clearReferences(workspace.getReferences(contentId));

// delete content
deleteMessage = workspace.delete(contentId);
out.println("Content deleted :" + deleteMessage);

// purge content
deleteMessage = workspace.purge(contentId);
out.println("<BR> Content purged:" + deleteMessage);
} else {
out.println("Could not delete content :" + contentName);
}

// ****************** Delete \ Purge Library Component example ******************

// define library component


String libraryComponentName = new String("LibraryComponent");

// find library component by name


DocumentIdIterator libraryComponentIterator =
workspace.findByName(DocumentTypes.LibraryComponent,libraryComponentName);

DocumentId libraryComponentId = null;

if (libraryComponentIterator.hasNext()){
libraryComponentId = libraryComponentIterator.nextId();

// clear all the references of library component


if(workspace.getReferences(libraryComponentId)!=null)
workspace.clearReferences(workspace.getReferences(libraryComponentId));

// delete library component


deleteMessage = workspace.delete(libraryComponentId);
out.println("Library Component deleted :" + deleteMessage);

// purge library component


deleteMessage = workspace.purge(libraryComponentId);
out.println("<BR> Library Component purged:" + deleteMessage);
} else {
out.println("Could not delete Library Component :" + libraryComponentName);

- 42 -
}

%>

4 Standards for names specified in all examples


As noted at the beginning of this paper, we have added italicized comments on most of the lines
of code to make it easier to understand both the code and flow.

A few standards have been set while writing the code for the API samples provided. These may
need to be modified and entered with values that are appropriate based on the specifics of the
Web Content Management artifacts on your system:

WebContent. This is taken as the standard library that we are setting in all scenarios
Site. Defined Site Name to be searched or the name already set for a site
NewSite. Name of new site created
SiteArea. Defined Site Area
NewSiteArea. Name of new site area created
Content. Defined Content Name
NewContent. Name of new content created
PublishedContent. Name of the published content
AuthoringTemplate. Defined Authoring Template
PresentationTemplate. Defined Presentation Template
LibraryComponent. Defined library component name
MyComponent. Defined component name
NewImageComponent. Name of new Image Component created
NewRichTextComponent. Name of new Rich Text Component created
Workflow. Defined Workflow name
WorkflowStageName. Defined Workflow Stage name
NewFileComponent. Name of new file resource created
Taxonomy. Defined Taxonomy name
NewParentTaxonomy. Name of new Taxonomy created
Category. Defined Category name to be searched
NewCategory. Name of new category created
Keyword. Defined Keyword
"/Web content/Site/SiteArea/Content. Content path
Source Library. Name of the Source Library defined
Target Library. Name of the Target Library defined
01/01/2008. Sample date selected for Start date
12/31/2009. Sample date selected for End date

5 Conclusion
This document has presented detailed code samples that you can use directly (copying/pasting)
to implement various scenarios using Web Content Management 6.1.0.x APIs, to extend usage of
Web Content Management. Hopefully we have targeted the most common issues faced by
customers in implementing and using APIs.

- 43 -
6 Resources
• IBM WebSphere Portal Version 6.1 Information Center: Web Content Management Edition:
http://publib.boulder.ibm.com/infocenter/wcmdoc/v6r0/index.jsp?topic=/com.ibm.lotus.wcm.do
c/wcm

• IBM WebSphere Portal Version 6.1.0 Information Center:


http://publib.boulder.ibm.com/infocenter/wpdoc/v6r1/index.jsp?topic=/com.ibm.wp.ent.doc_v61
01/reference/wpsdirstr.html

• WebSphere Portal and Lotus Web Content Management product documentation page:
http://www.ibm.com/developerworks/websphere/zones/portal/proddoc.html

• developerWorks WebSphere Portal Family wiki:


http://www-10.lotus.com/ldd/portalwiki.nsf

• Lotus Web Content Management forum:


http://www.ibm.com/developerworks/forums/forum.jspa?forumID=452&S_TACT=105AGX13&
S_CMP=LP

7 About the authors


Bhargav Thakker is a Staff Software Engineer on the WebSphere Web Content Management
team, working out of IBM’s Pune, India, facility. He worked extensively with Java/J2EE-based
Web applications prior to joining IBM in 2006. Bhargav is a Web Content Management certified
developer and holds a degree in Computer Engineering. He can be reached at
bthakker@in.ibm.com.

Soumitra Limaye is the Team Lead for the WebSphere Web Content Management team, working
out of IBM’s Pune, India, facility. He worked extensively with UNIX (admin-scripting) and other
Portal servers such as BEA WebLogic prior to joining IBM in 2006. Soumitra is a Web Content
Management certified developer and holds a degree in Computer Engineering. He can be
reached at soumitra.limaye@in.ibm.com

Trademarks
• IBM, Lotus, Web Content Management, and WebSphere are trademarks or registered
trademarks of IBM Corporation in the United States, other countries, or both.

• Java and all Java-based trademarks and logos are trademarks or registered trademarks of Sun
Microsystems, Inc. in the United States, other countries, or both.

• Other company, product, and service names may be trademarks or service marks of others.

- 44 -

S-ar putea să vă placă și