guideDocument copy

# Overview

The document copy feature allows users to copy an entire document from an active collaborative editing session or from the document storage. The copy of the document can be then edited independently from the original document but will still contain all the comments and suggestions from the source document.

# Prerequisites

  • An editor bundle needs to be uploaded.
  • The source document needs to be created after uploading the editor bundle.

# Usage

Copying a document means creating a new collaborative editing session using the document data from an already existing one on the CKEditor Collaboration Server. It is done by sending a request to POST /documents/{source_document_id} method from our REST API.
You can also use the endpoint to quickly create a copy of the document with comment and suggestion markers excluded without the need to modify the original document.

# Example

The example below has been prepared in Node.js.

  1. Run the following commands
mkdir cs-copy-example && cd cs-copy-example && npm init -y && npm i axios && touch copy.js
  1. Open cs-copy-example/copy.js and paste the following code snippet:
const crypto = require( 'crypto' );
const axios = require( 'axios' );

// Update with your credentials and application endpoint
const environmentId = 'txQ9sTfqmXUyWU5LmDbr';
const apiSecret = '4zZBCQoPfRZ7Rr7TEnGAuRsGgbfF58Eg0PA8xcLD2kvPhjGjy4VGgB8k0hXn';
const applicationEndpoint = 'https://33333.cke-cs.com';
const apiEndpoint = `${ applicationEndpoint }/api/v5/${ environmentId }/documents/${ sourceDocumentId }`;

// Set source and target document id
const sourceDocumentId = 'my_document_id_source';
const targetDocumentId = 'my_document_id_target';

const body = {
    'target_document_id': targetDocumentId,
    'omit_comments': false,
    'omit_suggestions': false
};

const CSTimestamp = Date.now();
const config = {
   headers: {
      'X-CS-Timestamp': CSTimestamp,
      'X-CS-Signature': generateSignature( apiSecret, 'POST', apiEndpoint, CSTimestamp, body )
   },
};

axios.post( apiEndpoint, body, config )
   .then( response => {
      console.log ( response.status );
   } ).catch( error => {
      console.log( error.message );
      console.log( error.response.data );
   } );

function generateSignature( apiSecret, method, uri, timestamp, body ) {
   const url = new URL( uri );
   const path = url.pathname + url.search;
   const hmac = crypto.createHmac( 'SHA256', apiSecret );

   hmac.update( `${ method.toUpperCase() }${ path }${ timestamp }` );

   if ( body ) {
      hmac.update( Buffer.from( JSON.stringify( body ) ) );
   };

   return hmac.digest( 'hex' );
}
  1. Update your credentials, the sourceDocumentId and the targetDocumentId values in the code snippet.

  2. Execute the node copy.js command.

After a successful response with a status code 201, you can open the editor with the same targetDocumentId as set in the snippet to see your copied content.

# Troubleshooting

  • In case of any operation while copying fails, e.g. fail while copying comments all data of a target document will be revoked. The data of the source document are not changed.
  • We suggest enabling Insight Panel, where you will find detailed logs from the copy process including exact reasons for any failed requests.

# More info

You will find more info in our REST API documentation.