Skip to main content

Update group manager

This example demonstrates how to update group manager in ONLYOFFICE DocSpace.

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 = {
Accept: 'application/json',
Authorization: `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
};

// Step 1: Create a new group and add members
async function createGroup(groupName, managerId, memberIds) {
const url = `${API_HOST}/api/2.0/group`;
const res = await fetch(url, {
method: 'POST',
headers: HEADERS,
body: JSON.stringify({
groupName: groupName,
groupManager: managerId,
members: memberIds,
}),
});

if (!res.ok) {
const t = await res.text();
console.log(`Group creation failed. Status code: ${res.status}, Message: ${t}`);
return null;
}

const json = await res.json();
const groupId = json?.response?.id;
console.log('Group created successfully:', groupId);
return groupId;
}

// Step 2: Reassign group ownership
async function reassignGroupManager(groupId, newManagerId) {
const url = `${API_HOST}/api/2.0/group/${groupId}`;
const res = await fetch(url, {
method: 'PUT',
headers: HEADERS,
body: JSON.stringify({ groupManager: newManagerId }),
});

if (!res.ok) {
const t = await res.text();
console.log(`Group manager reassignment failed. Status code: ${res.status}, Message: ${t}`);
return;
}

console.log(`Group ${groupId} manager reassigned successfully to ${newManagerId}`);
}

// Run
(async () => {
const manager_user_id = '10001';
const user_id = '10002';

// Step 1: Create a new group
const group_id = await createGroup('Development Team', manager_user_id, [manager_user_id, user_id]);

if (group_id) {
// Step 2: Reassign group ownership to user
await reassignGroupManager(group_id, user_id);
}
})();

Step 1: Create a group

A POST request is sent to /api/2.0/group with:

  • groupName: A new group name.
  • groupManager: The user ID of the group manager.
  • members: A list of user IDs to be added to the group.

The API returns a group ID, which is required for further operations.

async function createGroup(groupName, managerId, memberIds) {
const url = `${API_HOST}/api/2.0/group`;
const res = await fetch(url, {
method: 'POST',
headers: HEADERS,
body: JSON.stringify({
groupName: groupName,
groupManager: managerId,
members: memberIds,
}),
});

if (!res.ok) {
const t = await res.text();
console.log(`Group creation failed. Status code: ${res.status}, Message: ${t}`);
return null;
}

const json = await res.json();
const groupId = json?.response?.id;
console.log('Group created successfully:', groupId);
return groupId;
}

Step 2: Reassign group ownership

A PUT request is sent to /api/2.0/group/:id with:

  • group_id: The group ID.
  • groupManager: The user ID of the new group manager.
async function reassignGroupManager(groupId, newManagerId) {
const url = `${API_HOST}/api/2.0/group/${groupId}`;
const res = await fetch(url, {
method: 'PUT',
headers: HEADERS,
body: JSON.stringify({ groupManager: newManagerId }),
});

if (!res.ok) {
const t = await res.text();
console.log(`Group manager reassignment failed. Status code: ${res.status}, Message: ${t}`);
return;
}

console.log(`Group ${groupId} manager reassigned successfully to ${newManagerId}`);
}