Kloudless Blog

Kloudless Unified APIs enable you to code once and integrate many

Table of Contents

Introduction

This is a short guide on how to migrate from the older SharePoint Online REST API, which includes OneDrive for Business, to the new Graph API. It covers operations related to storage, such as files and folders. Migrating isn’t an easy task due to partial or missing documentation for both APIs and sometimes involves trail and error to figure out what works and what doesn’t.

  • We’ll first take a look how to retrieve SharePoint Online (SPO) and OneDrive for Business (ODB) file and folder metadata from the Graph API using SPO REST API identifiers.
  • We’ll then show how to achieve the same using the SPO REST API v2.0, that seems, at first glance, identical to the Graph API, but comes with it’s own hurdles.
  • Finally, we’ll discuss how the Kloudless API can help with integrating these Office 365 services.

Code included

This blog post includes code samples but doesn’t cover how to authenticate a client to use for the API requests. Please refer to Microsoft’s documentation for more information on how to accomplish this.

All the examples below use curl to perform requests to the Office 365 APIs.

Retrieving information about SPO & ODB items using SPO identifiers

The following Graph API URLs provide the most straightforward way to retrieve any list item’s metadata:

  • /sites/{site-id}/items/{item-id}
  • /sites/{domain}:/{site-path}:/items/{item-id}

The item-id in this case must be a ListItemUniqueId. Please refer to the Graph API docs for the specifics of what constitutes a valid site-id.

In the code below, we attempt to retrieve sufficient information from the SPO API to identify the item via the Graph API instead.

First, we make a request using the SPO API to retrieve an item’s metadata along with it’s ListId and UniqueId. We use the item’s site path in this first request.

The JSON data returned above provides the required Item ID in the UniqueId attribute, which we can use with the Graph API to retrieve the item’s data:

However, the response only returns the baseItem, and not the actual Drive Item we want to obtain. It also returns the ListItemId as the id and not the unique GUID. In order to actually obtain the SharePoint IDs and the driveItem, we need to use the following URL for the Graph API instead, which require the ListId:

  • /sites/{site-id}/lists/{list-id}/items/{item-id}
  • /sites/{domain}:/{site-path}:/lists/{list-id}/items/{item-id}

The initial response from the SPO REST API contains the list-id to use in the ListId attribute.

We select the sharepointIds attribute of the driveItem explicitly to obtain it in the response. To obtain additional listItem fields that contain metadata on the SPO ListItem object, expand the fields property as shown below:

Oddly, it isn’t possible to use the following OData query parameters in the above request:

This returns a malformed JSON string (likely a bug with the Graph API), that contains the sharepointIds and an error message:

This means that it isn’t possible to obtain both the Drive Item metadata as well as the SharePoint IDs in a single request. We’ll see how to solve this with the v2.0 API later below.

OneDrive for Business

To retrieve the data above on items in ODB rather than SPO, replace the site-path with the path to a user’s personal site, and ensure the hostname points to ODB (e.g. company-my.sharepoint.com):

CRUD operations

Perform DELETE, PATCH, and PUT operations directly on the driveItem as shown below, although it is possible to perform them on the listItem as well:

Using v2.0 of the SPO REST API

It turns out the v2.0 of the SPO REST API allows us to retrieve both the sharepointIds object and the driveItem information in a single request:

Note that this type of request was returning malformed data when using the Graph API. The v2.0 SPO REST API treats the sharepointIds field as a navigable property. The Graph API treats it as a normal property that can’t be extended.

The requests above also work with ODB as mentioned previously.

Similarly, the driveItem also allows DELETE, PATCH, and PUT operations on it:

Thoughts on the Graph and SPO REST APIs

Both the Graph API and the v2.0 SPO REST API interfaces constantly change. This results in some surprises since certain requests may cease to work or begin working unexpectedly. The requests above provide examples of both scenarios. Unfortunately, the documentation doesn’t clarify which features function as expected; if a request doesn’t work with the v1.0 Graph API, give the beta version a try.

How Kloudless helps

Similar to the Graph API, the Kloudless API provides a single interface to Office 365 SPO and ODB data. Rather than investigate the specifics of each of those APIs, use Kloudless to quickly integrate over 20 different cloud storage services with a single implementation. Kloudless also unifies the Graph API and SPO API data to ensure it returns the maximum amount of data available. Sign up for Kloudless here!

Categories: