Adding a Document to a Case Using FileNet CE Java API
In this sample code, a ByteArray stream, document name, document class, and CaseFolder ID are passed as method parameters. To attach a document to a case, it must be added to the case folder. Once added, it will appear as an attached document when the case is opened.
package com.sagt.PDFGenerator;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import javax.security.auth.Subject;
import org.apache.log4j.Logger;
import com.filenet.api.collection.ContentElementList;
import com.filenet.api.constants.AutoClassify;
import com.filenet.api.constants.AutoUniqueName;
import com.filenet.api.constants.CheckinType;
import com.filenet.api.constants.DefineSecurityParentage;
import com.filenet.api.constants.RefreshMode;
import com.filenet.api.core.Connection;
import com.filenet.api.core.ContentTransfer;
import com.filenet.api.core.Document;
import com.filenet.api.core.Domain;
import com.filenet.api.core.Factory;
import com.filenet.api.core.Folder;
import com.filenet.api.core.ObjectStore;
import com.filenet.api.core.ReferentialContainmentRelationship;
import com.filenet.api.exception.EngineRuntimeException;
import com.filenet.api.util.Id;
import com.filenet.api.util.UserContext;
/*
*
* @author P8Hub
*
*/
public class AttachDocument {
public static Logger log = Logger.getLogger(AttachDocument.class);
public String addDocumentWithStream(ByteArrayOutputStream fip, String docName, String docClass, String caseFolderId) throws EngineRuntimeException {
String userName = "CONNECTION_USER"; //CONNECTION_USER = "P8Admin";
String password = "CONNECTION_PASSWORD"; //CONNECTION_PASSWORD = "filenet";
String uri = "CONNECTION_URL"; //CONNECTION_URL = "http://100.10.10.213:9080/wsi/FNCEWS40MTOM/";
String jaasConfig = "CONNECTION_JAAS_PATH"; //CONNECTION_JAAS_PATH = "..\\PEPurge\\jaas.conf.WSI\\";
String oSName = "CONNECTION_OBJECT_STORE"; // CONNECTION_OBJECT_STORE = "CMTOS"
System.setProperty("java.security.auth.login.config", jaasConfig);
Connection connection = Factory.Connection.getConnection(uri);
Subject sub = UserContext.createSubject(connection, userName, password, "FileNetP8WSI");
UserContext.get().pushSubject(sub);
Domain domain = Factory.Domain.getInstance(connection, null);
ObjectStore objectStore = Factory.ObjectStore.fetchInstance(domain, oSName, null);
Folder folder = Factory.Folder.fetchInstance(objectStore, new Id(caseFolderId), null);
log.info("Case folder ID : " + folder.get_Id());
Document doc = Factory.Document.createInstance(objectStore, docClass);
ContentElementList contEleList = Factory.ContentElement.createList();
ContentTransfer ct = Factory.ContentTransfer.createInstance();
ct.setCaptureSource(new ByteArrayInputStream(fip.toByteArray()));
ct.set_ContentType("application/pdf");
ct.set_RetrievalName(docName);
contEleList.add(ct);
doc.set_ContentElements(contEleList);
doc.getProperties().putValue("DocumentTitle", docName);
doc.set_MimeType("application/pdf");
doc.checkin(AutoClassify.AUTO_CLASSIFY, CheckinType.MAJOR_VERSION);
doc.save(RefreshMode.REFRESH);
ReferentialContainmentRelationship rcr = folder.file(doc, AutoUniqueName.AUTO_UNIQUE, docName, DefineSecurityParentage.DO_NOT_DEFINE_SECURITY_PARENTAGE);
rcr.save(RefreshMode.REFRESH);
log.info("Version Series ID of the new document is : " + doc.get_VersionSeries().get_Id());
return doc.get_VersionSeries().get_Id().toString();
}
}Get P8 Hub🍁’s stories in your inbox
Join Medium for free to get updates from this writer.
Prerequisites
- Ensure that the required FileNet libraries, such as
cejava.jarandpe.jar, are included in your project’s classpath. - Have the Content Engine’s endpoint URL, username, and password ready.
Testing
- Run the program in a Java environment to establish a connection to the Content Engine and create document.
- Check the case folder to verify whether the PDF document has been created or not.







