Alert This post is over a year old, some of this information may be out of date.

#DevHack: Get the shared file info with Microsoft Graph

May 31, 2021

When you copy and share links from files, it creates a URL from which you cannot determine which file it is. For Squarl, I wanted to find out which file was behind a shared link to show the information about the file.

First of all, the shared links look like this:


First, I thought it would be a combination of splitting the URL with the site name and ID, but apparently, it was not. When I was going through the documentation, I saw the /shares/ endpoint. This endpoint seemed the one I needed to use to be able to get the file information.

Info Accessing shared DriveItems

To use the shares API, you need to use the share ID or the sharing URL. There is one more thing, and if you use the URL, you will need to base64 encode it.

Here is an example of how to use it with JavaScript/TypeScript.

const fileUrl = btoa(`<file-id>`);
// Remove the '=` sign and replace other characters
const resouce = `/shares/u!${fileUrl.slice(0, -1).replace(/\+/g, '-').replace(/\//g, '_')}/driveItem?$expand=listItem`;
const fileData = await graph.api(resource).version("v1.0").get();