Search code examples
c#microsoft-graph-apionedrive

Microsoft Graph - OneDrive API - Convert to jpg before download


I'm trying to access two OneDrive items. The first is a .docx and the second a .tif. I'd like to convert these to .jpg before downloading them.

When I run a request for the content...

// gets access to the service
GraphServiceClient graphServiceClient = await GetGraphServiceClient();

// get the item
DriveItem item = await graphServiceClient
    .Drive
    .Root
    .ItemWithPath("xxx")
    .Request()
    .GetAsync();

// set up query params
List<Option> options = new List<Option>();
options.Add(new QueryOption("format", "jpg"));

// get content stream for item, converted to .jpg format
item.Content = await graphServiceClient
    .Drive
    .Root
    .ItemWithPath("xxx")
    .Content.Request(options)
    .GetAsync();

This throws an Unknown Error Service Exception.

I thought it could have been a poorly formed request, but I can change that QueryOption format to pdf and it returns as you'd expect.

Here is the documentation for converting prior to download, showing that these formats are both supported to convert to jpg.

Does anyone know what could be going wrong here, why I can't retrieve a jpg but can retrieve a pdf?


Solution

  • Microsoft Graph v1.0 only supports converting to pdf. The ability to convert to other formats is only supported by the Beta endpoint.

    Supported in /v1.0:

    • format=pdf

    Supported in /beta:

    • format=pdf
    • format=html
    • format=glb
    • format=jpg

    Unfortunately, the OneDrive documentation does not make a distinction between production and beta functionality. For this reason, I don't recommend relying on it as a primary source. It is simply to easy to get tripped up just like this.

    In order to convert to jpg, you will need to use the Beta endpoint but setting the BaseUrl property to "https://graph.microsoft.com/beta". Just be sure and do this only for this scenario and not globally. The Beta endpoint is not stable enough for production so it should only be used for dev/test and "there is no other way" scenarios.