Building a report
This article walks you through the process of building a report from a template with the FastReport Cloud report processor.
Getting Started
You will need the following tools and features:
Knowledge of using API key in FastReport Cloud.
This article will skip additional information on authentication and authorization.
A C# code editor or a text editor such as Visual Studio Code.
Report template.
You can find how to upload a report template in the article Uploading a new template.
Active FastReport Cloud subscription.
Access to the Internet.
Note! This guide assumes that you already know how to develop your application in the C# programming language.
Note! The paragraphs above describe the recommended tools.
Instruction
You need a template identifier to build it, to get it, use the method GetFilesListAsync(String, Nullable<Int32>, Nullable<Int32>, String, Nullable<FileSorting>, Nullable<Boolean>, Nullable<Boolean>, System.Threading.CancellationToken)
public async Task<string> GetTemplateId(HttpClient httpClient) { ITemplateFoldersClient templateFoldersClient = new TemplateFoldersClient(httpClient); ITemplatesClient templatesClient = new TemplatesClient(httpClient); FileVM rootFolder = await templateFoldersClient.GetRootFolderAsync(null); IEnumerable<TemplateVM> templates = await templatesClient.GetFilesListAsync(rootFolder.Id, 0, 10); TemplateVM template = templates.First(); return template.Id; }
In this example, the function requests the user's default workspace root directory, then requests 10 templates, and returns the first one.
To build a report, a directory will be needed to put the report to. Request the report root directory by using the method GetRootFolderAsync(String, System.Threading.CancellationToken).
public async Task<string> GetReportsRoot(HttpClient httpClient, string subscriptionId = null) { IReportFoldersClient reportFoldersClient = new ReportFoldersClient(httpClient); FileVM result = await reportFoldersClient.GetRootFolderAsync(subscriptionId); return result.Id; }
In this example, the function requests the root directory and the workspace identifier can be omitted, in which case the root directory for the user's default workspace will be returned.
To build a report, use the method PrepareAsync(String, PrepareTemplateVM, System.Threading.CancellationToken).
public async Task<string> BuildReport(HttpClient httpClient, string folderId, string templateId, string fileName) { ITemplatesClient templatesClient = new TemplatesClient(httpClient); PrepareTemplateVM task = new PrepareTemplateVM() { Name = Path.ChangeExtension(fileName, ".fpx"), FolderId = folderId }; ReportVM result = await templatesClient.PrepareAsync(templateId, task); return result.Id; }
In this example, the function creates a report preparation task.
Note! Although the report has not yet been built, it already has an identifier assigned to it. In a while, the builder queue will reach this task, and the report will be built.
If FolderId is not specified, the prepared report will be saved in the root folder.
To get information about the report, use the method GetFileAsync(String, System.Threading.CancellationToken).
public async Task<ReportVMStatus> CheckStatus(HttpClient httpClient, string reportId) { IReportsClient reportsClient = new ReportsClient(httpClient); ReportVM result = await reportsClient.GetFileAsync(reportId); return result.Status.GetValueOrDefault(); }
In this example, the function requests the report by its identifier and returns the status. You need to wait for the Success status, check the status every few seconds.
We check the status in a loop and download the file.
int tries = 10; FileStatus status; do { status = await CheckStatus(httpClient, reportId); tries--; } while (status != FileStatus.Success && tries > 0); var report = await DownloadReport(httpClient, reportId);
To download the report, use the method GetReportAsync(String, System.Threading.CancellationToken).
public async Task<byte[]> DownloadReport(HttpClient httpClient, string reportId) { IDownloadClient downloadClient = new DownloadClient(httpClient); FileResponse file = await downloadClient.GetReportAsync(reportId); using(MemoryStream ms = new MemoryStream()) { file.Stream.CopyTo(ms); return ms.ToArray(); } }
In this example, the function requests the file and copies it into memory.