1

I'd like to download a file attached to a PlannerTask. I already have the external references but I can't figure out how to access the file.

An external reference is a JSON object like this:

{
  "https%3A//contoso%2Esharepoint%2Ecom/sites/GroupName/Shared%20Documents/AnnualReport%2Epptx":
  {
    // ... snip ...
  }
}

I've tried to use the following endpoint

GET /groups/{group-id}/drive/root:/sites/GroupName/Shared%20Documents/AnnualReport%2Epptx

but I get a 404 response. Indeed, when I use the query in Graph Explorer it gives me a warning about "Invalid whitespace in URL" (?).

A workaround that I've found is to use the search endpoint to look for files like this:

GET /groups/{group-id}/drive/root/search(q='AnnualReport.pptx')

and hope the file name is unique.

Anyway, with both methods I need extra information (ie. the group-id) that may not be readily available by the time I have the external reference object.

What is the proper way to get a download url for a driveItem that is referenced by an external reference object in a PlannerTask?

Do I really need the group-id to access such file?

1 Answers1

0

The keys in external references are webUrl instances, so they can be used with the /shares/ endpoint. See this answer for details on how to do it.

When you get a driveItem object, the download url is available from AdditionalData: driveItem.AdditionalData["@microsoft.graph.downloadUrl"]. You can use WebClient.DownloadFile to download the file on the local machine.

Here is the final code:

var remoteUri = "https%3A//contoso%2Esharepoint%2Ecom/sites/GroupName/Shared%20Documents/AnnualReport%2Epptx";
var localFile = "/tmp/foo.pptx";

string sharingUrl = remoteUri;
string base64Value = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(sharingUrl));
string encodedUrl = "u!" + base64Value.TrimEnd('=').Replace('/','_').Replace('+','-');

DriveItem driveItem = m_graphClient
    .Shares[encodedUrl]
    .DriveItem
    .Request()
    .GetAsync().Result;

using (WebClient client = new WebClient())
{
    client.DownloadFile(driveItem.AdditionalData["@microsoft.graph.downloadUrl"].ToString(), 
                        localFile);
}