Skip to main content

Scripts

  • 4 minutes to read

This document describes the most common scenarios of using report scripts.

Enable Scripts

In a new Report and Dashboard Server installation, scripts are disabled for security reasons. It is not recommended that you change this behavior.

If you need to disable security restrictions for any reason (for example, to save logs), go to the Administrative Panel’s General Settings section and set the Report Script Execution Mode option to Unrestricted.

Enable Scripts

Script Editor

Each report element has its own set of events that can be handled by the Script Editor. To handle an event of a report element, do the following.

  1. Click the Scripts button (ScriptEditorIcon) located on the Report Designer’s toolbar.
  2. In the invoked Script Editor, specify the report control and an available event for this control.

    SriptEditor

    After the event is chosen, a code template is generated in the current scripting language.

  3. To check for errors in the report’s script, click the Validate button (aspx-script-editor-validate).

    If an error is found, the string that contains this error is marked with the aspx-script-editor-error icon. When a mouse pointer hovers over this icon, the error text is displayed.

Enable Logging

To use NLog for debug tracing when scripts are enabled, specify the corresponding nlog configuration in the Web.config file of the worker service. This file is located in the directory where you installed the Report and Dashboard Server (“C:\Program Files (x86)\DevExpress\Report Server\Web\Worker” by default).

<logger name="ScriptingNamespace.ScriptingReport" minLevel="Trace" writeTo="file" />

The following example illustrates how to use NLog in a script.

static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();

private void Report_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e) {
    logger.Info("This is a test.");
}

Access User Information

To access information about the current user in report scripts, use the ReportContext functionality. The following code displays the current user e-mail in a report.

private void label1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e) {
    if(ReportContext.Current != null) {
        ((XRLabel)sender).Text = ReportContext.Current.UserEmail;
    }
}

The report context information is available only when a report is being viewed on a client. When a report is generated by the Scheduler, the ReportContext.Current property will return null.

Access Data Source Settings

The following code handles a report’s DataSourceDemanded event to access the report’s data source in scripts.

private void XtraReport_DataSourceDemanded(object sender, EventArgs e) {
    var report = sender as XtraReport;
    var dataSource = report.DataSource as 
        DevExpress.ReportServer.Infrastructure.Data.ReportServerDataSource;
    dataSource.Filters["Products"] = "[CategoryID] = " + report.Parameters["CategoryId"].Value;
}

The following code accesses and customizes parameters of a stored procedure in scripts.

using System.Linq;

private void XtraReport_DataSourceDemanded(object sender, EventArgs e) {   
    var report = sender as XtraReport;
    var dataSource = report.DataSource as 
        DevExpress.ReportServer.Infrastructure.Data.ReportServerDataSource;
    var Beginning_Date = dataSource.Parameters["Sales by Year"].First(p => 
        p.Name.Equals("@Beginning_Date"));
    var Ending_Date = dataSource.Parameters["Sales by Year"].First(p => 
        p.Name.Equals("@Ending_Date"));
    if (report.Parameters[0].Value.ToString() == "Today") {
        var now = DateTime.Now;
        Beginning_Date.Value = new DateTime(now.Year, now.Month, now.Day);
        Ending_Date.Value = ((DateTime)Beginning_Date.Value).AddDays(1).AddTicks(-1);
    }
}

The following code accesses the settings of a multi-value data source parameter and selects all available values in the ParametersRequestBeforeShow event handler.

using System.Linq;
using DevExpress.XtraReports.Parameters;
using DevExpress.Data.Browsing;
using DevExpress.ReportServer.Infrastructure.Data;

private void XtraReport_ParametersRequestBeforeShow(object sender, ParametersRequestEventArgs e) {
    var report = sender as XtraReport;
    var parameter = report.Parameters["CategoryNames"];
    var paramSource = ((DynamicListLookUpSettings)parameter.LookUpSettings).DataSource 
        as ReportServerDataSource;
    ((IListAdapter)paramSource).FillList(null);
    var dataContext = ((IServiceProvider)report).GetService(typeof(DataContext)) as DataContext;
    var values = LookUpHelper.GetLookUpValues(parameter.LookUpSettings, dataContext).Select(x =>
        (string)x.Value).ToArray();
    parameter.Value = values;
}