Preparing a report
This article describes how to build a report from a template using the FastReport Corporate Server report processor.
Getting started
You will need the following tools and features:
Knowledge on how to use API key in FastReport Corporate Server.
This article does not include the information about authentication and authorization.
A C# code editor or a text editor like Visual Studio Code.
A report template.
How to upload a report template can be found in the article Upload a new template.
Active subscription for FastReport Corporate Server.
Internet access.
Please, note: This tutorial assumes that you already know how to develop your application with the C# programming language.
Please, note: The descriptions above are provided for the recommended tools.
Instructions
You need a template identifier to build it; to get it, use the <xref:FastReport.Cloud.ITemplatesClient.GetFilesListAsync(System.String,System.Nullable{System.Int32},System.Nullable{System.Int32})> method
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 root directory of the user's first subscription, then it requests 10 templates and returns the first one.
To prepare a report, you need a directory where you can store the report. Request the root report directory by using the <xref:FastReport.Cloud.IReportFoldersClient.GetRootFolder(System.String)> method.
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, the subscription identifier can be omitted, in which case the root directory for the user's first subscription will be returned.
To build a report, use the <xref:FastReport.Cloud.ITemplatesClient.PrepareAsync(System.String,FastReport.Cloud.PrepareTemplateTaskVM)> method.
public async Task<string> BuildReport(HttpClient httpClient, string folderId, string templateId, string fileName) { ITemplatesClient templatesClient = new TemplatesClient(httpClient); PrepareTemplateTaskVM task = new PrepareTemplateTaskVM() { Name = Path.ChangeExtension(fileName, ".fpx"), ParentFolderId = folderId }; ReportVM result = await templatesClient.PrepareAsync(templateId, task); return result.Id; }
In this example, the function creates a task for preparing a report.
Please, note: Although the report has not yet been built, at this stage it already has an identifier, so after a while the builder will deal with this task and the report will be built.
To get information about the report, use the <xref:FastReport.Cloud.IReportsClient.GetFileAsync(System.String)> method.
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 a report by its identifier and returns the status. You need to wait for the status <xref:FastReport.Cloud.ReportVMStatus.Success>. Check status every few seconds.
To download the report, use the <xref:FastReport.Cloud.IDownloadClient.GetReportAsync(System.String)> method.
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 a file and copies it into memory.