Wednesday, April 15, 2009

Creating Content using WCM API

This time will tell how you can create a content using WCM API. Before I want you to see my sample code I would like to show the WCM API method to get what is required to create content into Web content management through WCM API.

JavaDoc description for Create Content method in WCM API:

createContent (In API JavaDoc)

Content createContent(DocumentId authoringTemplateId,
DocumentId parentSiteAreaId,
DocumentId siblingId,
int position)
throws DocumentCreationException,
AuthorizationException,
IllegalDocumentTypeException

Creates a new Content object, based on an AuthoringTemplate in the parent SiteArea. The template ID that is specified must be for a template that already exists in the WCM system.

If the ChildPosition is ChildPosition.PREVIOUS or ChildPosition.NEXT, then the siblingId parameter must be specified. If it is not, then an IllegalArgumentException will be thrown. If the ChildPosition is ChildPosition.START or ChildPosition.END then the siblingId parameter, if it is not null, will be ignored.

Parameters:
authoringTemplateId - the authoring template ID
parentSiteAreaId - the parent site area ID
siblingId - the DocumentId of the sibling document, if any. May be null, according to the rules stated
position - the position of the child (docId)

Returns:
a new Content object based on the specified template

Throws:
DocumentCreationException - if the object cannot be created
AuthorizationException - if the user does not have access
IllegalDocumentTypeException - if specified template is not an AuthoringTemplate, the parentSiteArea is not a SiteArea, or the sibling is not a Content or ContentLink.

**** MY SAMPLE CODE STARTS HERE *****

String userName = USER_BALA;
String password = PASS_BALA;
String wcmLibraryName= LBRY_Test;
String contentName = CNTNT_Test;
String contentTitle = CNTNT_Test_Title;
String siteArea= SA_Test;
String authoringTemplate= AT_Test;

// Getting workspace object.
Workspace oWorkspace = WCM_API.getRepository().getWorkspace(username, password);

// Setting current document library
oWorkspace.setCurrentDocumentLibrary(oWorkspace.getDocumentLibrary(wcmLibraryName));

DocumentId oDocumentIdContent = Util.getDocumentId(oWorkspace, DocumentTypes.
Content, contentName);

// Check whether content is already existing or not
if( oDocumentIdContent==null){
/*
* Getting Site Area document id.
*/
DocumentId oDocumentIdSiteArea = Util.getDocumentId(oWorkspace, DocumentTypes.
SiteArea, siteArea);

if(oDocumentIdSiteArea!=null){
// Get the authoring template document id.
DocumentId oDocumentIdAuthoringTemplate = Util.getDocumentId(oWorkspace, DocumentTypes.AuthoringTemplate, authoringTemplate);

if(oDocumentIdAuthoringTemplate!=null){
// Creating content here ...
oContent = oWorkspace.createContent(oDocumentIdAuthoringTemplate, oDocumentIdSiteArea, null, ChildPosition.END);
oContent.setName(contentName);
oContent.setTitle(contentTitle );
}
}
}

String errors[] = oWorkspace.save(oContent);
for(int x=0;x LessThan errors.length;x++) {
System.
out.println("Errors while saving the content into workspace "+errors[x]);
}

**** UTIL METHODS STARTS HERE ****

public static DocumentId getDocumentId(Workspace oWorkspace, DocumentType oDocumentType, String fileName) throws Exception{
DocumentId oDocumentID = null;
DocumentIdIterator oDocumentIdIteratorContents = oWorkspace.findByName(oDocumentType,fileName);
if(oDocumentIdIteratorContents.hasNext()){
oDocumentID= (DocumentId) oDocumentIdIteratorContents.next();
}
return oDocumentID;
}
**** UTIL METHODS ENDS HERE ****

**** MY SAMPLE CODE ENDS HERE ****

I hope this will give clear idea about how to add a content into IBM Web Content Management System using WCM API.

4 comments:

Anonymous said...

Thanks !!

Currao said...

Very interesting article!!
Thank You!!!

Shashank said...

thanks for the article.
could u please also demonstrate how to add various content components in the content?

Anonymous said...

I am facing the below issue.

I have updated authoring template x with 1 text item. now i am trying to apply this AT x to an existing content using the method
content.setAuthoringTemplateID(template)
which give me an error.

how can this achieved through the API