Microsoft Graph allows to work with OneDrive files and SharePoint documents using DriveItem resource type. Using this entity you can read information about a file, such as size, content, file type, and so on (here is a link to the resource type documentation).
You can get DriveItem object from a specific Drive, Site, Group, or user's connected OneDrive. But for all of these operations you need to know identifiers of parent resources, e.g. drive id, or site id (documentation on how to get drive item).
But what if the file is located in a custom document library, in a subfolder, and on a subsite? And you have only absolute url for the document, like https://your-tenant.sharepoint.com/sites/your-site/your-subsite/DocLibrary/subfolder/file.ext?
If you try to use the approach described in the documentation, you'll need to:
- Correctly split the url to have site relative url, document library url, and file path in that library. And the algorithm for that might be really complicated, especially without additional knowledge about site contents.
- Get all drives from the site and find drive and its id associated with the document library (unfortunately there is no API to get drive by path like /sites/{site-id}/drives:/path-to-doc-library)
- Get driveItem using request like /sites/{your-tenant}:/{site-relative-url}/drives/{drive-id}/root:/{item-path}
But... Good news everyone! And the name of it - MS Graph shares endpoint!
We can actually transform our absolute url into sharing encoded url and use it with /shares endpoint to get driveItem.
So, first, let's encode the url, using the code from official documentation:
Now, when we have the sharing url we can get the driveItem:
Conclusion
The conclusion here is one sentence: if you need to get drive item by absolute url, use shares endpoint!That's all for today!
Have fun!
Comments