+1 800 256 3608 (toll-free in North America) or +49 7531 9060-10| service@combit.com

Report Server: Retrieve export result as stream via REST API

With the help of the ExportAsync function of the PreparedReport class, a report can be generated/exported via the REST API of the combit Report Server. The result is an ExportResult object whose method DownloadFilesAsync can be used to download the generated report to the file system. This process is carried out in the ClientApi Sample programming example installed with the List & Label installation - here is an excerpt:

...
// Exports a report and downloads the files
private async Task ExportAndDownloadFiles(PreparedReport preparedExport)
{
    ExportResult result = await preparedExport.ExportAsync();

    MessageBox.Show("The export was completed and the files are ready for download, please choose a directory to save the file(s).");

    if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
    {
        await result.DownloadFilesAsync(folderBrowserDialog.SelectedPath, CancellationToken.None);
        if (MessageBox.Show("The report has been downloaded, would you like to open it?", "ClientAPI", MessageBoxButtons.YesNo) == DialogResult.Yes)
        {
            string reportFilePath = Path.Combine(folderBrowserDialog.SelectedPath, result.FirstPageFileLink.RelativeFilePath);
            Process.Start(new ProcessStartInfo(reportFilePath) { UseShellExecute = true });
        }
    }
}
...

However, to avoid having to download the files to the file system, it is a good idea to load the report directly into a stream. This can be very useful, for example, if you are using an Azure App Service and there is no access to the file system or the result is to be transferred directly to a document management system via a stream, etc.

In order to download the report into a stream, you can use the property ExportedFileLinks. All files of the report are contained here after the export and can be loaded directly into a stream via the DownloadUri property using a separate HttpClient:

...
// Exports a report and load the files into stream
public async Task DownloadFilesIntoStreamAsync(PreparedReport preparedExport)
{
    ExportResult result = await preparedExport.ExportAsync();

    List<Task> downloadJobs = new List<Task>();
    foreach (ExportedFileLink fileEntry in result.ExportedFileLinks)
    {
        Task downloadJob = Task.Run(async () =>
        {
            using (var httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Add("X-ReportServer-ClientId", "<Report Server REST API User>");
                httpClient.DefaultRequestHeaders.Add("X-ReportServer-ClientToken", "<Report Server REST API ClientToken>");

                using (var response = await httpClient.GetAsync(fileEntry.DownloadUri))
                {
                    Stream outputStream = new MemoryStream();
                    await response.Content.CopyToAsync(outputStream);
                }
            }
        });
        downloadJobs.Add(downloadJob);

    }
    await Task.WhenAll(downloadJobs);
}
...

It is important that authentication is taken into account, as a separate HttpClient is used. This can be implemented via additional header entries X-ReportServer-ClientId and X-ReportServer-ClientToken.

Further information on the REST API of the combit Report Server can be found in the two namespaces combit.ReportServer.ClientApi and combit.ReportServer.ClientApi.Objects. General information on the REST API can be found in the User Manual in chapter REST API.

© combit GmbH