.Net Client

Basics

jsreport.Client is c# client for the jsreport REST API. It represents convenient way to remotely access jsreport server instance and invoke report rendering using simple c# code. It is compatible with the on premise, jsreportonline as well as .net local instances. The jsreport.Client is implemented as .NET Standard library so it should run pretty much everywhere the .Net Standard 1.6 or higher is implemented.

Get started

The most common scenario is that you first design a report template using jsreport studio and save it on the jsreport server. Later you want to initiate the rendering from c# and provide the real data.

var rs = new ReportingService("http://localhost:5488");
var report = await rs.RenderByNameAsync("Invoice", new
{
    Id = 123,
    From = "Erich Gamma",
    To = "Martin Fowler"
});

The usage with jsreportonline is pretty much the same, just with different url and with provided credentials.

var rs = new ReportingService("https://[subdomain].jsreportonline.net", "email", "password");

The rendering output includes the report stream report.Content and additional report.Meta object that contains many helpful information like content type, number of produced pdf pages or logs.

Advanced rendering

If you want to have the full control on the template rendering you can use RenderAsync overload which accepts RenderRequest instance allowing you to fill bunch of other options.

You should use this overload if you for example don't store templates in jsreport server but rather in your own storage. In this case you can send the whole template specification without referencing the existing one by its name.

Here you can see how you can convert html to xlsx and use a templating engine to dynamically inject values into the output without storing template in jsreport store.

var report = await rs.RenderAsync(new RenderRequest()
{
    Template = new Template()
    {
        Recipe = Recipe.HtmlToXlsx,
        Engine = Engine.Handlebars,
        Content = "<table><tr><td>Hello {{message}}</td></td>"
    },
    Data = new
    {
        message = "world!"
    }
});

Logs

The output report.Meta.Logs includes valuable information from the rendering process. You can find there not only the jsreport logs but also javascript errors or console.log entries. This is very valuable when troubleshooting the rendering without using jsreport studio.

Types

The types like RenderRequest or Template are installed using the package jsreport.Types. The releases of this package match the jsreport releases therefore you should be able to install correct types if you for some reason need to connect to an older version of jsreport.

Pdf utils

The types contains complete set of entities and properties used to define the request. This means you can for example configure pdf-utils extension to merge multiple pdfs during rendering.

rs.RenderAsync(new RenderRequest {
    Template = new Template()
    {
        Content = "Helo world",
        Engine = Engine.Handlebars,
        Recipe = Recipe.ChromePdf,
        Chrome = new Chrome
        {
            MarginTop = "2cm"
        },
        PdfOperations = new List<PdfOperation>()
        {
            new PdfOperation()
            {
                Type = PdfOperationType.Merge,
                Template = new Template
                {
                    Content = "header",
                    Engine = Engine.None,
                    Recipe = Recipe.ChromePdf
                }
            }
        }
    }
});

Overwrites

The provided types only includes the attributes used by the official extensions. However you can also simply use RenderRequest.Overwrites property to pass your own data to the request.

var report = await rs.RenderAsync(new RenderRequest()
{
    ...
    Overwrites = new
    {
        Template = new
        {
            MyTemplateCustomProperty = "foo"
        }
    }
});

Timeouts

ReportingService uses .NET HttpClient to make http calls. This library has default timeout of 100seconds for a request. This doesn't have to be enough for bigger reports therefore you may need to change this default timeout.

reportingService.HttpClientTimeout = TimeSpan.FromMinutes(10);

Odata

Waiting for .net core support in Simple.OData.Client - follow this issue.

jsreport API for doing CRUD on entities is based on odata and you can use great Simple.OData.Client library to consume it.

var client = new ODataClient("http://localhost:5488/odata");
var metadata = client.GetMetadataAsync().Result;

To understand jsreport entities checkout jsreport API metadata at https://[jsreport address]/odata/$metadata. You can find many examples how to work with odata cliet here.

Filtering

var template = await client.For<Template>()
        .Filter(x => x.shortid == "Report1")
        .FindEntryAsync();

Modifying entries

var template = await client.For<Template>()
        .Filter(x => x.shortid == "Report1")
        .FindEntryAsync();

await client.For<Template>()
        .Key(template._id)
        .Set(new {
                _id = template._id,
                content = "Some modified content"
            })
        .UpdateEntryAsync();