Skip to main content

Duplicate files and folders

This example demonstrates how to duplicate one or more files and folders in ONLYOFFICE DocSpace using the API. The duplicated items will appear in the same location with a modified title.

Before you start

  1. Replace https://yourportal.onlyoffice.com and YOUR_API_KEY with your actual DocSpace portal URL and API key. Ensure you have the necessary data and permissions to perform migration operations.
  2. Before you can make requests to the API, you need to authenticate. Check out the Personal access tokens page to learn how to obtain and use access tokens.
Full example
// Set API base URL
const API_HOST = 'https://yourportal.onlyoffice.com';
const API_KEY = 'your_api_key';

// Headers with API key for authentication
const HEADERS = {
Authorization: `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
};

// File and folder IDs to duplicate
const FILE_IDS = [111111, 222222];
const FOLDER_IDS = [333333, 444444];

// Step 1: Duplicate specified files and folders
function duplicateFilesAndFolders(fileIds, folderIds) {
const url = `${API_HOST}/api/2.0/files/fileops/duplicate`;
const data = {
fileIds: fileIds,
folderIds: folderIds,
};

return fetch(url, {
method: 'PUT',
headers: HEADERS,
body: JSON.stringify(data),
})
.then(async (res) => {
if (res.status === 200) return res.json();
const text = await res.text();
console.log(`Duplication failed. Status code: ${res.status}, Message: ${text}`);
return null;
})
.catch(() => null);
}

// Run
duplicateFilesAndFolders(FILE_IDS, FOLDER_IDS);

How it works

A PUT request is sent to /api/2.0/files/fileops/duplicate.

You must pass:

  • fileIds: A list of file IDs to be duplicated.
  • folderIds: A list of folder IDs to be duplicated.

The response contains the status and metadata of the duplication operations.