Search code examples
sharepointmicrosoft-graph-apioffice365

Fetch a file from Office365 sharepoint graph api with a sharing link


I have a bunch of sharing link from my org (https://mycorp.sharepoint.com/:x/:y/<some_folder>/<sharing_id>

I find myself unable to fetch the file from graph api with this technique:

  • GET https://graph.microsoft.com/v1.0/sites/mycorp.sharepoint.com:/sites/<some_folder>
  • GET https://graph.microsoft.com/v1.0/sites/{site_id}/drives
  • GET https://graph.microsoft.com/v1.0/sites/{site_id}/drives/{drive_id}/root:/<sharing_id>/content

Is there something I'm missing ?


Solution

  • You need to expand the sharing link first see https://learn.microsoft.com/en-us/graph/api/shares-get?view=graph-rest-1.0&tabs=http

    If your using the SDK you need to first encoded the sharedlink then you can expand and get the downloadurl from driveitem eg

    string sharedlink = "https://xxxx-my.sharepoint.com/:w:/p/xxx/EXTw9StT51hKicApjyP3dQoBS2b8Z3FfaTv32Un5zeTbZA?e=ObtbwL";
    string base64Value = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(sharedlink));
    string encodedUrl = "u!" + base64Value.TrimEnd('=').Replace('/', '_').Replace('+', '-');
    var sharedFile = graphclient.Shares[encodedUrl].DriveItem.GetAsync().Result;
    var fileurl = sharedFile.AdditionalData["@microsoft.graph.downloadUrl"].ToString();