#DevHack: Get the shared file info with Microsoft Graph
This post is over a year old, some of this information may be out of date.
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:
https://squarl.sharepoint.com/:x:/s/Squarl/<file-id>
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.
{{< blockquote type="info" text="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(`https://squarl.sharepoint.com/:x:/s/Squarl/<file-id>`);// Remove the '=` sign and replace other charactersconst resouce = `/shares/u!${fileUrl.slice(0, -1).replace(/\+/g, '-').replace(/\//g, '_')}/driveItem?$expand=listItem`;const fileData = await graph.api(resource).version("v1.0").get();
Related articles
Show the Share Action on your Page Layouts
Creating all day events with the Microsoft Graph on Office 365
Building daemon or service app with the Microsoft Graph API
Report issues or make changes on GitHub
Found a typo or issue in this article? Visit the GitHub repository to make changes or submit a bug report.
Comments
Let's build together
Manage content in VS Code
Present from VS Code