Collector task


In this chapter you will learn about tasks, the heart of a collector


Collectors essentially build on a standard feature of the Enonic framework - tasks. Tasks provide out-of-the-box support for running background jobs with ease. Also, tasks can be scheduled via the Enonic framework.


Tasks are essentially JS controllers, following a specific pattern.

Your app contains an example task within in the following file structure:

Application code:
    sample-collector/ (1)
     sample-collector.ts (2)
     sample-collector.xml (3)
1 The task folder
2 The task controller: Same name as folder
3 The task schema. Same name as folder

For more detais on tasks, visit the task documentation.


The task must be explicitly declared in the collectors.json file.

The task name is the name of the collector, it must be unique within the app. Changing the name of the task will break the link between Explorer and the collector.
	"displayName": "Sample Collector (Edit in collectors.json)",
	"formLibraryName": "SampleCollector",
	"formAssetPath": "collector/form/SampleCollector.esm.js",
	"taskName": "sample-collector"

As you can see from the above file, the task name is listed per collector definition.

Task schema

The task schema is defined in sample-collector.xml.

This file basically defines an interface for tasks. It should basically look the same for all collectors (remember, you may have multiple collectors within a single app).

The following fields are defined:


Which collection is calling the collector


Registered ID of this collector


Configuration based on the collector form


Default language, as specified by the collector

This is what the schema looks like:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
	<description>Sample Collector (edit in sample-collector.xml)</description>
		<input name="collectionId" type="TextLine">
			<label>Collection ID</label>
			<occurrences minimum="0" maximum="1"/>
		<input name="collectorId" type="TextLine">
			<label>Collector ID</label>
			<occurrences minimum="1" maximum="1"/>
		<input name="configJson" type="TextLine">
			<label>Config JSON</label>
			<occurrences minimum="1" maximum="1"/>
		<input name="language" type="TextLine">
			<occurrences minimum="0" maximum="1"/>

Task controller

The actual task controller supplied by the starter is called sample-collector.ts.

This task controller is written in TypeScript, and will be compiled to JavaScript by the build script.

We’ll not dive into the exact details of how the controller gets its data, but rather look at the two most common parts of any collector task:


Every collector needs to implement the run() function, and accept the standard parameters - as defined above.

import {Collector} from '/lib/explorer';

export function run({
}) {
    const collector = new Collector<CollectorConfig>({
        collectionId, collectorId, configJson, language


Another common element for any collector is to store documents in Explorer, for indexing and search.

Remember, a document is essentially a JSON file.
Example code for persisting a document
collector.persistDocument(documentToPersist, {
        documentTypeName: 'starter_doctype'
        message: "Sweet, it worked"
} catch (e) {
    log.error(`message:${e.message}`, e);
    collector.addError({message: `${e.message}`});
Feel free to study the full task controller code for more details.

For an extensive task example - check out the Webcrawler collector code. The Webcrawler is shipped with Explorer by default.

Check out the Collector API documentation for a complete list of available functions.