Skip to main content

Change file or folder ownership

This example demonstrates how to transfer ownership of files and/or folders in ONLYOFFICE DocSpace to another user using the API. This is useful for reassigning responsibility when a project changes owners or a team member leaves.

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 your DocSpace portal URL, access token, and IDs
const BASE_URL = 'https://yourportal.onlyoffice.com';
const API_KEY = 'your_api_key';

const HEADERS = {
Authorization: API_KEY,
'Content-Type': 'application/json',
};

// Step 1: Define file and/or folder IDs to transfer
const fileIds = [12345]; // Replace with your file IDs
const folderIds = [67890]; // Replace with folder IDs if needed
const newOwnerId = 'user_uuid_here'; // New owner's UUID

const payload = {
fileIds: fileIds,
folderIds: folderIds,
userId: newOwnerId,
};

// Step 2: Send the ownership change request
function changeOwnership(payload) {
const url = `${BASE_URL}/api/2.0/files/owner`;

return fetch(url, {
method: 'POST',
headers: HEADERS,
body: JSON.stringify(payload),
})
.then((res) => {
if (res.status === 200) return res.json();
const text = await res.text();
console.log(`Ownership update failed. Status code: ${res.status}, Message: ${text}`);
return null;
})
.then((data) => {
const result = data?.response ?? [];
console.log('Ownership successfully updated for the following items:');
result.forEach((entry) => {
const title = entry?.title ?? 'Unnamed';
const shared = entry?.access?.shared ?? false;
console.log(`- ${title} (Shared: ${shared})`);
});
})
.catch((err) => {
console.log(`Ownership update error: ${err}`);
});
}

// Run the method
changeOwnership(payload);

Step 1: Define items for ownership transfer

Prepare a payload specifying:

  • fileIds: List of file IDs to transfer ownership.
  • folderIds: List of folder IDs to transfer ownership (optional).
  • userId: UUID of the new owner.
payload = {
"fileIds": [12345],
"folderIds": [67890],
"userId": "user_uuid_here"
}

Step 2: Send a POST request to change ownership

A POST request is sent to /api/2.0/files/owner with the payload. The API responds with a list of updated items and whether they are still shared.

function changeOwnership(payload) {
const url = `${BASE_URL}/api/2.0/files/owner`;

return fetch(url, {
method: 'POST',
headers: HEADERS,
body: JSON.stringify(payload),
})
.then((res) => {
if (res.status === 200) return res.json();
const text = await res.text();
console.log(`Ownership update failed. Status code: ${res.status}, Message: ${text}`);
return null;
})
.then((data) => {
const result = data?.response ?? [];
console.log('Ownership successfully updated for the following items:');
result.forEach((entry) => {
const title = entry?.title ?? 'Unnamed';
const shared = entry?.access?.shared ?? false;
console.log(`- ${title} (Shared: ${shared})`);
});
})
.catch((err) => {
console.log(`Ownership update error: ${err}`);
});
}