Mavention Workspace has the ability to easily change the image of a group (or as we like to call them: Workspace). This will set the new image visible on all platforms including Microsoft Teams (if it is a Teams workspace), Outlook and SharePoint. This sounds like an easy to write code-module. And when I began writing the module, I thought just the same. However, there are some tricky parts in communicating with the Graph API if you want to send files. In this blog, I will elaborate on how to upload images using the Microsoft Graph API.
On this page I have found a way to convert Base64 strings to binary data and return a blob which we can send to the Graph API endpoint. The answer of Jeremy also contains information that helps you understand what is actually going on in the function. However, this function will not work if you are calling it with a base64 string that is gathered by something like this:
This is because the file is rendered with 'data:image/{imageType};base64/' infront of it. So in order to directly gather the base64 string from an HTML element we have to remove this part of the string. This can be done by replacing:
With:
Graph API endpoint
In order to upload or replace an image and make it available on all platforms, we have to update the values that already exist. The endpoint we are looking for is described here. As you can read in the documentation there are some permissions that have to be set according to your needs:- User.ReadWrite -> For the profile picture of the signed in user
- User.ReadWrite.All -> For the profile picture of any user in the organization, on behalf of the app's identity
- Group.ReadWrite.All -> For the profile picture of a group
- Contacts.ReadWrite -> For the photo of a contact
Call the Graph API
This is where the tricky parts starts. We have to give some value to the endpoint, and as you can read in the Microsoft Docs it cannot be a Base64 string, which would be a lot easier. Instead, the endpoint expects binary data of the image. But how to convert a Base64 string to binary data? It's easy to say a Blob will do some magic. But then again; how to put this huge string into a Blob? Firstly the base64 string has to be gathered from the uploaded file. Once a file is selected using a simple <input type="file"/> we can access the data. This can be done with the following code:var file = document.querySelector('#fileInput').files[0];
public uploadImage() { getBase64String(file).then(base64Image => { const groupId = 'The group ID here'; const request = { method: 'PUT', url: 'https://graph.microsoft.com/v1.0//groups/' + groupID + '/photo/$value', responseType: 'application/json', data: base64Image }; this.$http(req).then(result => { // Image has been set }, (err) => { // Image has not been set }); }); }
public getBase64String(file: any) { return new Promise((resolve, reject) => { const reader = new FileReader(); reader.readAsDataURL(file); reader.onload = () => resolve(reader.result); reader.onerror = error => reject(error); }); }
var file = document.querySelector('#fileInput').files[0];
var byteCharacters = atob(base64Image);
var byteCharacters = atob(base64Image.replace(/^data:image\/(png|jpeg|jpg);base64,/, ''));