Create report
Products: FastReport Cloud, FastReport Corporate Server, FastReport Publisher
This article describes the process of creating a report from a template using the Service solutions report processor.
Getting started
You will need the following tools and facilities:
Know how to use API key in Service solutions.
This article will skip over additional information on authentication and authorization.
C# code editor or text editor, e.g., Visual Studio Code.
Report template.
To learn how to upload a report template, see the Upload new template article.
Active subscription to one of the following products: FastReport Cloud, FastReport Corporate Server, or FastReport Publisher.
Internet access.
Important! These guides assume that you already know how to develop your application in the C# programming language.
Note. The items above describe recommended tools.
Instruction
You need the template ID to create the template. To get it, use the following method GetFilesListAsync(string, int?, int?, string, FileSorting?, bool?, bool?, 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 root directory of the user's default workspace, then requests 10 templates and returns the first one.
To generate a report, you will need a directory to place the report in. Request the report root directory by using the following method GetRootFolderAsync(string, 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, the workspace ID can be omitted. In this case, the root directory for the user's default workspace will be returned.
To create a report, use the following method PrepareAsync(string, PrepareTemplateVM, 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 task to create a report.
Important! The report has not been generated yet, but it has already been assigned an identifier. After some time, the builder's turn will come to this task and the report will be created.
If you do not specify FolderId, the generated report will be saved to the root folder.
For information about the report, use the following method GetFileAsync(string, 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 a report by its ID and returns the status. You must wait for the Success status, check it every few seconds.
Check the status in the 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 following method GetReportAsync(string, 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 a file and copies it to memory.