Export library

Contents

This API provides functions for creating and importing node-exports.

Usage

Add the following to your build.gradle file:

dependencies {
  include xplibs.export
}

Add the import statement to your code:

import exportLib from '/lib/xp/export';

You are now ready to use the API.

Functions

exportNodes

Creates a node export.

Parameters

An object with the following keys and their values:

Name Type Attributes Default Description

sourceNodePath

string

Source nodes path

exportName

string

Export name

nodeResolved

function

<optional>

A function to be called before export starts with number of nodes to export

nodeExported

function

<optional>

A function to be called during export with number of nodes exported since last call

batchSize

number

<optional>

1000

Controls how many nodes are loaded into memory simultaneously during export. Higher value may speed up export but also increase memory usage

Returns

object : Node export results

Examples

import {exportNodes} from '/lib/xp/export';

// Export content nodes.
const result = exportNodes({
    sourceNodePath: '/content',
    exportName: 'export-1',
    nodeExported: (i: number) => {
    },
    nodeResolved: (i: number) => {
    }
});
import {exportNodes} from '/lib/xp/export';

// Export content nodes with a custom batch size.
const result = exportNodes({
    sourceNodePath: '/content',
    exportName: 'export-batch',
    batchSize: 25
});
// Information about exported nodes.
const expected = {
    exportedNodes: [
        '/content'
    ],
    exportedBinaries: [
        'binaryPath [ref]'
    ],
    exportErrors: [
        'some error'
    ]
};

importNodes

Imports nodes from a node export or from application resource files. Optionally pre-transforms node XML node files with XSLT before import.

During node export, current permissions are included in the export file. But when importing, you may need to apply permissions according to your new environment/parent permissions. includePermissions parameter is used to control this behavior.

Parameters

An object with the following keys and their values:

Name Type Attributes Default Description

source

string or object

Either name of nodes-export located in exports directory or application resource key

targetNodePath

string

Target path for imported nodes

xslt

string or object

<optional>

XSLT file name in exports directory or application resource key. Used for XSLT transformation

xsltParams

object

<optional>

Parameters used in XSLT transformation

includeNodeIds

boolean

<optional>

false

Set to true to use node IDs from the import, false to generate new node IDs

includePermissions

boolean

<optional>

false

Set to true to overwrite Node permissions with permissions from the import, false to merge permissions with new parent permissions

versionAttributes

object

<optional>

Optional attributes attached to the node version of every imported node

nodeResolved

function

<optional>

A function to be called before import starts with number of nodes to import

nodeImported

function

<optional>

A function to be called during import with number of nodes imported since last call

nodeSkipped

function

<optional>

A function to be called during import with number of nodes skipped since last call

Returns

object : Node import results

Examples

import {importNodes} from '/lib/xp/export';

// Import from application resource files
const importedNodes = importNodes({
    source: resolve('/import'),
    targetNodePath: '/content',
    xslt: 'transform.xslt',
    xsltParams: {k: 'v'},
    includeNodeIds: true,
    includePermissions: true,
    nodeImported: (i: number) => {
    },
    nodeResolved: (i: number) => {
    },
    nodeSkipped: (i: number) => {
    }
});
import {importNodes} from '/lib/xp/export';

// Import from an export in exports directory
const importedNodes = importNodes({
    source: 'export-1',
    targetNodePath: '/content'
});
// Information about imported nodes.
const expected = {
    addedNodes: [
        '/added'
    ],
    updatedNodes: [
        '/updated'
    ],
    skippedNodes: [],
    importedBinaries: [
        'binaryPath [ref]'
    ],
    importErrors: [
        {
            exception: 'com.enonic.xp.lib.export.ImportHandlerTest$NoStacktraceException',
            message: 'error',
            stacktrace: []
        }
    ]
};

list

Lists the available node exports.

Parameters

None.

Returns

object : { exports: [{ name: string }] } — names of all available exports.

Example

import {list} from '/lib/xp/export';

const result = list();
// result.exports === [{ name: 'export-1' }, { name: 'export-2' }]

Contents

Contents