2 practical things about the new Group Files API


Recently Microsoft updated the Office 365 Unified Files API. Here are two things that you have to know before you start using these new APIs with Office 365 Groups.

Getting files using the new Group Files API

The easiest way to get Group Files using the new Files API is to:

1. Get your Groups

GET https://graph.microsoft.com/beta/me/joinedGroups

Authorization: Bearer xyz
Accept: application/json;odata.metadata=full

The trick is to set the Accept header to application/json;odata.metadata=full which will cause the response to provide you with a navigation link (stored in the drive@odata.navigationLink property) to the files of your Group which is less error-prone than constructing it yourself.

REST URLs to access Group Files

2. Get Group files

Execute a GET request to the URL returned by the drive@odata.navigationLink property in the previous request. Append /root to that URL:

GET https://graph.microsoft.com/beta/contoso.onmicrosoft.com/groups/14cb9281-5d0c-4bfa-99da-1eef714c2d2b/drive/root

Authorization: Bearer xyz
Accept: application/json;odata.metadata=full

The number of files within the Group will be returned by the folder.childCount property.

If you want to get files stored in the root directory append /children to the URL:

GET https://graph.microsoft.com/beta/contoso.onmicrosoft.com/groups/14cb9281-5d0c-4bfa-99da-1eef714c2d2b/drive/root/children

Authorization: Bearer xyz
Accept: application/json;odata.metadata=none

After executing this request, you will see the list of files stored in the root folder of your Group.

List of files stored in a Group retrieved using the REST API

Uploading files to a Group

The new Group Files endpoint uses the OneDrive API which is documented at https://dev.onedrive.com/index.htm. The documentation contains all the details of how the API works. Unfortunately at this moment not all OneDrive capabilities are supported in OneDrive for Business. Be sure that you read all of the documentation before you start wondering why things are not working for you.

Uploading files to a Group works differently in the new API. Where in the old API we first had to create an empty file and then upload its contents, in the new API we need to do this in a single request as the OneDrive API doesn’t support creating empty files.

Following is how you upload a binary file to a Group:

$http({
    url: 'https://graph.microsoft.com/beta/contoso.com/groups/14cb9281-5d0c-4bfa-99da-1eef714c2d2b/drive/root/children/' + fileName + '/content',
    method: 'PUT',
    headers: {
        'Accept': 'application/json;odata.metadata=full'
    },
    data: getBinaryFileContents(base64FileContents), 
    transformRequest: []
}).success(function (result) {
    // process file creation information
}).error(function (err) {
    // handle error
});

The example is a complete AngularJS request that you need to issue in order to upload a binary file to a Group. Assuming your file would be a base64-encoded string you could use the following function to turn the string into a binary array required by the API:

function getBinaryFileContents(base64FileContents) {
    var raw = window.atob(base64FileContents);
    var rawLength = raw.length;
    var array = new Uint8Array(new ArrayBuffer(rawLength));
    
    for(var i = 0; i < rawLength; i++) {
        array[i] = raw.charCodeAt(i);
    }
    
    return array;
}

If your file contents are a raw string, you can skip the first line of the function.

Summary

Recently Microsoft updated the Office 365 Unified Files API. The new API is based on the OneDrive API and works differently than the Files API provided to us initially. Although the OneDrive API offers more consistency across the different workloads of Office 365 and Office.com, not all of its capabilities are currently supported in Office 365. The few tips provided in this post should help you get started. If you have any feedback regarding the new APIs don’t hesitate to post it to the Office 365 APIs UserVoice site.

Others found also helpful: