Recipes

This page is dedicated to list common questions, tricks and solutions (mainly got from the Seal Report Forum or from our experience on site).

We assume that you are familiar with the Seal Report architecture and components (Advanced users are welcomed here !).

Add your Recipe

Do you have a new recipe to share ?
As the site for the documentation is part of the solution, you are welcome to Pull a Request at GitHub.

Adding translation files

Languages files are located in the /Settings Repository sub-folder.
The files have the CSV format and are stored in UTF8 (not ANSI). The column containing the translations has the header with the Two Letter ISO Language Name (e.g. it for Italy).

Tanslations.csv contains the public translations for the product (Web Report Server and Report Result).
To add a new language, create a new translations file in the directory and name it with the Two Letter ISO Language Name before the extension (e.g. Translations.it.csv for Italy).

In addition, you can translate labels specific to your repository (date sources, connections, column names, report names, dashboard names, folder names, etc.) in the RepositoryTranslations.csv file.
You can generate working files for your repository translations using the Server Manager application: Use the 2 menus Tools->Export ... in CSV to generate the files, then copy and paste the records in the RepositoryTranslations.csv file according to your needs.

Once the new files saved, you can then specify the default language using the Server Manager application, click on the menu Configuration->Server Configuration... and specify the default culture of the product.

In reports, the culture can also be overridden per View (Culture Name property in the root View parameter), thus you can generate the same report in different languages.

Make sure that the new file follows the original CSV format and is saved in UTF8.
Test your new translations with the Web Report Server, just change your culture in your profile.

Filtering records based on security

It is sometimes useful to change the result set returned by a report per user logged (e.g. The salesman can see only his orders).
This may be implemented by the use of a Razor Script in the Additional WHERE Clause property of the relevant table in your Data Source.

Example for Northwind:

In the Northwind Data Source, select the Orders table, then use the following Additional WHERE Clause:
@using Seal.Model
@using Seal.Helpers
@{
    MetaTable table = Model;
    string restriction = Environment.UserName; //This gives the windows user of the process running the engine
    if (table.Source.Report != null && table.Source.Report.SecurityContext != null)
    {
        var securityContext = table.Source.Report.SecurityContext; //User is logged through a Web Report Servert and has a context
        restriction = securityContext.Name; //Name of the user set during the login
        restriction = securityContext.WebUserName; //Name got from the login window
        //securityContext.SecurityGroups; //List of security groups set for the user
        if (securityContext.BelongsToGroup("Default Group")) { //Test if the user belongs to a group
            //Special restriction here
        }
    }
    string result = string.Format("Orders.EmployeeID in (SELECT EmployeeID FROM Employees WHERE LastName={0})", Helper.QuoteSingle(restriction));
    }
@Raw(result)
                                
In line 16, a dynamic restriction is set using the name of the user logged.
Thus, when the Orders table is involved in a model, the table will be automatically filtered with this restriction.
You may adapt this example to fit your requirements. There might be also other ways to implement dynamic security...

SQL Server Stored Procedures

Using Stored Procedures to generate a report is often requested. However as a SP is not a table, it cannot be directly defined as a table in your Data Source.
The call of a SP depends also on your database engine.
The following sample shows 3 different methods to call a SP in SQL Server, it must be adapted for other database engine.

SQL Server Stored Procedures

Check the report 21-SQL Server Stored Procedure with parameters in the /Reports/Samples Repository sub-folder.

Integrating Seal Report

Here are some recipes if you consider to use SR in your own .Net application (Fat Client, ASP .Net, Console, etc.), there is no simple answer and the integration depends on your requirements and target architecture.

SealLibrary Assembly

If you have a project referencing the SealLibrary.dll assembly, you can execute a report with the following code (got from the TestAndSamples project of the solution)

            Repository repository = Repository.Create();
            Report report = Report.LoadFromFile(@"C:\ProgramData\Seal Report Repository\Reports\Search - Orders.srex", repository);
            ReportExecution execution = new ReportExecution() { Report = report };
            execution.Execute();
            while (report.IsExecuting) {
                System.Threading.Thread.Sleep(100);
            }
            string resultPath = execution.GenerateHTMLResult();
            Process.Start(resultPath); //result contains the file path of the HTML report result
                                

The TestAndSamples project contains also code showing how to create and execute a report from scratch.

Generating report results on the fly...

Check the interesting report 34-Task Generate several Report Results in the /Reports/Samples Repository sub-folder.
The reports has a task that generates a report result by country using the Search - Orders report.
@using Seal.Model
@using Seal.Helpers
@using System.IO
@using System.Data
@{
    ReportTask task = Model;
	TaskHelper helper = new TaskHelper(task);
	Repository repository = task.Report.Repository;
    string result = "1"; //Set result to 0 to cancel the report.

	//Load report to execute
	Report report = Report.LoadFromFile(Path.Combine(repository.ReportsFolder, "Search - Orders.srex"), repository);
	task.Report.LogMessage("Load done");
	//Create an output for a folder
	var folderDevice = repository.Devices.First(i => i is OutputFolderDevice);
	var output = report.AddOutput(folderDevice);
	output.FolderPath = repository.ReportsFolder;
	
	//Load list of countries
	string connectionString = task.Connection.FullConnectionString;
	string sql = @"select distinct Country from Customers";

	var toProcess = helper.DatabaseHelper.LoadDataTable(connectionString, sql);
	foreach (DataRow row in toProcess.Rows)
	{
		string country = (string)row[0];
		//Set the restriction
		var restriction = report.Models[0].GetRestrictionByName("Customer Country");
		restriction.Prompt = PromptType.None;
		restriction.Operator = Operator.Equal;
		restriction.EnumValues.Clear();
		restriction.EnumValues.Add(country);
		
	    ReportExecution execution = new ReportExecution() { Report = report };
        report.OutputToExecute = output;
        report.CurrentViewGUID = output.ViewGUID;
		
		//Set result file name and culture
		output.FileName = "Search - Orders for " + country;		
		report.ExecutionView.CultureName = "English";
		
		task.Report.LogMessage("Executing report for '{0}'", country);
		execution.Execute();	
		while (report.Status != ReportStatus.Executed) {
			System.Threading.Thread.Sleep(1000);
		}
		task.Report.LogMessage("File generated in '{0}'", output.FileName);
	}

}
@Raw(result)
                                

Seal Web Interface API

You can also install a Seal Web Server and use the Seal Web Interface API to execute reports from JavaScript.
Here is a sample to execute a report in another window from a browser (requires JQuery):

function executeReport(path) {
    var server = "https://sealreport.org/demo/"
    $.post(server +"SWILogin", {
        user: "", // The user name
        password: "" // The password
    })
        .done(function (data) {
            var f = $('<form method="post" target="_blank" />').appendTo('body');
            f.attr('action', server + "SWExecuteReport");
            f.append($('').attr('name', 'path').attr('value', path));  //the report path,  e.g. "/Samples/01-Simple list (Products).srex"
            f.children('input').attr('type', 'hidden');
            f.submit();            
        });
    return false;
}

executeReport("/Overview - Sales.srex");
                                

Dynamic report and chart title

This recipe explains how to modify a report or a chart title to display the current restriction or the current page value.
Using the Report Designer, create a report having several pages and a Chart JS.
Add a restriction in the model (e.g. prompt a value for Order Year)

To modify the report title:
Select the root Views node in the main Tree View and edit the DisplayName property.

@using Seal.Model
@using System.IO
@{
    Report report = Model;
    string result = Path.GetFileNameWithoutExtension(report.FilePath) + " " + report.Models[0].GetRestrictionByName("Order Year").DisplayValue1 + ' ' + DateTime.Now.ToShortDateString();
}
@Raw(result)
                                

The report title will contain the restriction chosen by the user and the current date.

To modify the chart title:
Select the Chart JS View, then edit the Custom template property to change the code that defines the chart title (around the line number 70):

                title: {
                    display: true,
                    text: '@Raw(Helper.ToJS("Sales for " + page.PageTable[1,0].HTMLValue))', //Value 1 of Page Table
//HTML for first restriction    Raw(Helper.ToJS("Sales for " + reportModel.GetRestrictionByName("Order Year").DisplayValue1))',               
//for Enum                      Raw(Helper.ToJS("Sales for " + reportModel.GetRestrictionByName("Category").EnumDisplayValue))', 
                    position: '@view.GetValue("chartjs_title_position")'
                },
                                

page.PageTable[1,0].HTMLValue returns the first value of your current page table, but you can use any relevant value from your model or report (e.g. reportModel.GetRestrictionByName("Order Year").DisplayValue1 for a restriction value.

This may be easily adapted for the other chart type.

Contribute to Seal Report

Enhance the documentation and samples

Want to share your experience or to add a new topic ? Something is not clear in the documentation ? Do you have an interesting recipe ?
As the site for the documentation is part of the solution, you are welcome to Pull a Request at GitHub.

Add translation files

Do you have a translation files not yet supported by Seal Report ?
Check first the recipe Adding translation files.
As the translations are part of the project, feel free to Pull a Request at GitHub.

And more...

You are also welcome to help us in several different areas:
  • Beta testing.
  • Libraries of Data Sources and Reports on standard databases.
  • Additional View templates.
  • New features development.
Please feel free to contact us at Ariacom or use GitHub if you are interested in contributing to Seal Report.