Search code examples
sharepointoffice365onedrivemicrosoft-graph-api

Download SharePoint file from Microsoft Graph API using "sourcedoc" ID


I'm trying to download a SharePoint file from the MS Graph /drive/items/xxxx API endpoint. The link I have is in the format https://company-my.sharepoint.com/personal/drive_name/_layouts/15/WopiFrame.aspx?sourcedoc=%7B<GUID>%7D, but the graph API needs some other OneDrive file ID to successfully fetch file metadata.

I have the correct permission scopes, and if I enumerate drive contents I can see the file there.

Is there any way to convert the sourcedoc guid into what is required by the API, or alternatively get file metadata by SharePoint guid?


Solution

  • what you're after is the Shares API. Given a URL like the one you used in your example you need to convert it to a share id value using this algorithm.

    So for your example the id be:

    u!aHR0cHM6Ly9jb21wYW55LW15LnNoYXJlcG9pbnQuY29tL3BlcnNvbmFsL2RyaXZlX25hbWUvX2xheW91dHMvMTUvV29waUZyYW1lLmFzcHg_c291cmNlZG9jPSU3QjxHVUlEPiU3RA
    

    With this value you can then query the item's metadata using:

    https://graph.microsoft.com/v1.0/shares/u!aHR0cHM6Ly9jb21wYW55LW15LnNoYXJlcG9pbnQuY29tL3BlcnNvbmFsL2RyaXZlX25hbWUvX2xheW91dHMvMTUvV29waUZyYW1lLmFzcHg_c291cmNlZG9jPSU3QjxHVUlEPiU3RA/root