Sunday, February 13, 2011

Content Creation with the IBM WCM API - works for version 6, 6.1,7

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

<% // retrieve repository Repository repository = WCM_API.getRepository(); //Creating a workspace Workspace workspace = repository.getWorkspace("username","password"); // Set library workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("LibraryName")); // define authoring template String authoringTemplateName = new String("Authoring Template"); // define parent sitearea String parentSiteAreaName = new String("SiteArea Name"); // define workflow String workflowName = new String("Workflow Name"); DocumentId authoringTemplateId = null; DocumentId parentSiteAreaId = null; DocumentId siblingId = null; DocumentId workflowId = null; // find authoring template by name DocumentIdIterator authoringTemplateIterator = workspace.findByName(DocumentTypes.AuthoringTemplate,authoringTemplateName); if (authoringTemplateIterator.hasNext()){ authoringTemplateId = authoringTemplateIterator.nextId(); System.out.println("Authoring Template found : " + authoringTemplateName + "
");
}else {
out.println("Could not find Authoring Template: " + authoringTemplateName + "
");
}

// find sitearea by name
DocumentIdIterator siteAreaIterator = workspace.findByName(DocumentTypes.SiteArea,parentSiteAreaName );
if (siteAreaIterator.hasNext()){
parentSiteAreaId = siteAreaIterator.nextId();
out.println("Sitearea Found : " + parentSiteAreaName + "
");
}else {
out.println("Could not find Sitearea : " + parentSiteAreaName + "
");
}

// find workflow by name
DocumentIdIterator workflowIterator = workspace.findByName(DocumentTypes.Workflow,workflowName);
if (workflowIterator.hasNext()){
workflowId = workflowIterator.nextId();
out.println("Workflow found : " + workflowName + "
");
}else {
out.println("Could not find Workflow: " + workflowName + "
");
}
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);

workspace.save(newContent);

DocumentIdIterator contentIterator = workspace.findByName(DocumentTypes.Content,"Almost there");
DocumentId contentId = null;
if(contentIterator.hasNext()) {
contentId = contentIterator.nextId();
}

//Get the content using getById
Content content = (Content)workspace.getById(contentId);

//Approving the Content
newContent.nextWorkflowStage();

System.out.println("Content page '" + parentSiteAreaName + newContent + "' moved to the approved state
");
String[] saveMessage = workspace.save((Document)newContent);

workspace.save(content);


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

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

%>

2 comments:

  1. Nice tutorial. Thanks for this.
    Could you also provide a sample code to create a content with file resource component?

    ReplyDelete
  2. sure will do...give me a few days :-)

    ReplyDelete