Returning a Custom Result with Automation

Updated: January 2, 2020

This page is scheduled for review and update. Check back soon for updated content!

As automatic marshalling is not implemented into Automation server and client, only Document(s) and Blob(s) can be manipulated. Therefore, the way to return a custom type is to encapsulate the value in a Blob.

Below is an example, based on the results returned by the method QueryAndFetch.

  • Operation code

    import java.util.Iterator;
    import java.util.Map;
    import net.sf.json.JSONArray;
    import net.sf.json.JSONObject;
    import org.nuxeo.ecm.automation.core.Constants;
    import org.nuxeo.ecm.automation.core.annotations.Context;
    import org.nuxeo.ecm.automation.core.annotations.Operation;
    import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
    import org.nuxeo.ecm.automation.core.annotations.Param;
    import org.nuxeo.ecm.core.api.Blob;
    import org.nuxeo.ecm.core.api.CoreSession;
    import org.nuxeo.ecm.core.api.IterableQueryResult;
    import org.nuxeo.ecm.core.api.impl.blob.StringBlob;
    import org.nuxeo.ecm.core.query.sql.NXQL;
    @Operation(id = QueryAndFetch.ID, category = Constants.CAT_FETCH, label = "QueryAndFetch", description = "Sample to show how to return a blob for any result type.")
    public class QueryAndFetch {
        public static final String ID = "Test.QueryAndFetch";
        protected CoreSession session;
        @Param(name = "query")
        protected String query;
        protected String lang = NXQL.NXQL;
        public Blob run() throws Exception {
            IterableQueryResult result = session.queryAndFetch(query, lang);
            Iterator<Map<String, Serializable>> it = result.iterator();
            JSONArray array = new JSONArray();
            while (it.hasNext()) {
                Map<String, Serializable> item =;
                JSONObject object = new JSONObject();
            return new StringBlob(array.toString(), "application/json");
  • Registering this operation

    <?xml version="1.0"?>
    <component name="">
      <extension target="org.nuxeo.ecm.core.operation.OperationServiceComponent"
        <operation class="" />
  • Sample code to use the result from the operation

    HttpAutomationClient client = new HttpAutomationClient("http://localhost:8080/nuxeo/site/automation");
    Session session = client.getSession(ADMINISTRATOR, ADMINISTRATOR);
    Blob response = (Blob) session.newRequest(QueryAndFetch.ID).set("query", "select ecm:uuid, dc:title, common:icon from Document").execute();
    String json =;
    JSONArray array = JSONArray.fromObject(json);
    System.out.println("Objects received : " + array.size());

We'd love to hear your thoughts!

All fields required