跳到主要内容

Manage user photo

This example demonstrates how to upload and delete user profile photos in ONLYOFFICE DocSpace using API requests. Managing user profile pictures helps create a personalized and professional experience within the system.

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: Upload user photo
async function uploadUserPhoto(userId, photoUrl) {
const url = `${API_HOST}/api/2.0/people/${userId}/photo`;
const res = await fetch(url, {
method: 'PUT',
headers: HEADERS,
body: JSON.stringify({ files: photoUrl }),
});

if (res.ok) {
console.log(`Failed to upload user photo: ${res.status} - ${text}`);
} else {
const text = await res.text();
console.log(`Failed to upload user photo: ${res.status} - ${text}`);
}
}

// Step 2: Delete user photo
async function deleteUserPhoto(userId) {
const url = `${API_HOST}/api/2.0/people/${userId}/photo`;
const res = await fetch(url, { method: 'DELETE', headers: HEADERS });

if (res.ok) {
console.log(`Failed to delete user photo: ${res.status} - ${text}`);
} else {
const text = await res.text();
console.log(`Failed to delete user photo: ${res.status} - ${text}`);
}
}

// Run
(async () => {
const user_id = '10001';
const photo_url = 'https://github.com/ONLYOFFICE/DocSpace-client/blob/master/public/appIcon-192.png?raw=true';

await uploadUserPhoto(user_id, photo_url);
await deleteUserPhoto(user_id);
})();

Step 1: Upload a new photo

A PUT request is sent to /api/2.0/people/:userid/photo with:

  • user_id: The user ID.
  • files: The URL to a new photo.
async function uploadUserPhoto(userId, photoUrl) {
const url = `${API_HOST}/api/2.0/people/${userId}/photo`;
const res = await fetch(url, {
method: 'PUT',
headers: HEADERS,
body: JSON.stringify({ files: photoUrl }),
});

if (res.ok) {
console.log(`Failed to upload user photo: ${res.status} - ${text}`);
} else {
const text = await res.text();
console.log(`Failed to upload user photo: ${res.status} - ${text}`);
}
}

Step 2: Delete user photo

A DELETE request is sent to /api/2.0/people/:userid/photo with:

  • user_id: The user ID.
async function deleteUserPhoto(userId) {
const url = `${API_HOST}/api/2.0/people/${userId}/photo`;
const res = await fetch(url, { method: 'DELETE', headers: HEADERS });

if (res.ok) {
console.log(`User photo deleted successfully for ${userId}`);
} else {
const text = await res.text();
console.log(`Failed to delete user photo: ${res.status} - ${text}`);
}
}