Examples: Bulk action queries and scripts

These JavaScript code examples use the Content Platform Engine Java™ API and Java class libraries. You can use these libraries and JavaScript bulk actions to quickly write powerful scripts. Each script example has an associated query example.
Here are the prerequisites for running the examples:
  • At least one object store is created.
  • Some documents with content exist in the object store.

Bulk action to download document content

This JavaScript example downloads the document content to a specified directory.

Adapt the following items in the script example:
Output directory Change the output directory in the script. Because the JavaScript code is executed on a Content Platform Engine server, the content download directory that you specify must satisfy the following requirements:
  • It exists on the server.
  • It is one to which the server has write permissions.
After the query runs, look at the output folder to verify that the document content was downloaded as expected.

Query example: SELECT TOP 100 This FROM Document

Script example:

importClass(Packages.com.filenet.api.collection.ContentElementList);
importClass(Packages.com.filenet.api.core.ContentTransfer);
importClass(java.io.FileOutputStream);
importClass(java.io.InputStream);
importClass(java.lang.Byte);

function OnCustomProcess (CEObject)
{
   CEObject.refresh();
   var ce = CEObject.get_ContentElements();
   if(ce.size() > 0)
   {   
        var ct  = ce.get(0); 
        var folderName = "C://Temp/Content/"; 	// output directory 
        this._downloadContent(folderName, ct);
   }
}
 
function _downloadContent(folderName, ct)
{
    var out = new FileOutputStream(folderName + ct.get_RetrievalName());
    var docLen = ct.get_ContentSize().intValue();
    var buf = java.lang.reflect.Array.newInstance(Byte.TYPE, docLen)
    var stream = ct.accessContentStream();
 
    stream.read(buf, 0, docLen);
    out.write(buf);
    out.flush();
    stream.close();
    out.close();
}

            

Bulk action to promote documents

This JavaScript example demotes the major version of the document and then promotes a minor version.

Query example: SELECT TOP 5 This FROM Document

Script example:

importClass(Packages.com.filenet.api.constants.RefreshMode);
function OnCustomProcess (CEObject)
{
   CEObject.demoteVersion();
   CEObject.save(RefreshMode.REFRESH);
   CEObject.promoteVersion();
   CEObject.save(RefreshMode.REFRESH);
}
            

Bulk action to delete documents

This JavaScript example deletes a document.

Important: Create a document with a document title such as “Test Delete Document”.

Query example: SELECT This FROM Document WHERE DocumentTitle = 'Test Delete Document'

Script example:

importClass(java.lang.System);
importClass(Packages.com.filenet.api.property.Properties);
importClass(Packages.com.filenet.api.constants.RefreshMode);
 
function OnCustomProcess (CEObject)
{
    System.out.println("Executing Delete Document script");
    CEObject.delete();
    CEObject.save(RefreshMode.REFRESH);
    System.out.println("Document deleted successfully");
}
            

Bulk action to fetch document property values

This JavaScript example fetches the document properties and prints the information. This information is logged in an application log file.

Query example: SELECT TOP 5 This FROM Document

Script example:

importClass(java.lang.System);
importClass(Packages.com.filenet.api.property.Properties);
 
function OnCustomProcess (CEObject)
{
   System.out.println("Executing Fetch Properties script");
   CEObject.refresh();
   var props = CEObject.getProperties();
 
   System.out.println("ClassName................... " + CEObject.getClassName());
   System.out.println("Has DocumentTitle property.. " + props.isPropertyPresent("DocumentTitle")); 
   System.out.println("Propertites size............ " + props.size()); 
}
            

Bulk action to set document property values

This JavaScript example shows how to update the document title property.

Adapt the following items in the query example:
Document Id Search for the Id of the document whose title you want to change.

Query example: SELECT This FROM Document WHERE Id = <Document-Id>

Script example:

importClass(Packages.com.filenet.api.property.Properties);
importClass(Packages.com.filenet.api.constants.RefreshMode);

function OnCustomProcess (CEObject)
{
    CEObject.refresh();
    CEObject.getProperties().putValue("DocumentTitle", "Test1");
    CEObject.save(RefreshMode.REFRESH);
}