The requested page is not available for the requested platform. You are viewing the content for Default platform.

Scripts

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

Disable Security in Scripts

By default, you are not permitted to access the file system and fill the data source using report scripts. It is not recommended that you change this default behavior.

If, for any reason, you need to disable the security restrictions (e.g., to enable logging), set the disableScriptSecurity flag to true in the Web.config file located at the directory where you installed the Report and Dashboard Server ("C:\Program Files (x86)\DevExpress\Report Server\Web\Worker" by default).

<configuration>
<!-- … -->
  <appSettings>
    <add key="disableScriptSecurity" value="true"/>
  </appSettings>
<!-- … -->
</configuration>

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 displayed Script Editor, specify a report control and one of events available for this cotnrol.

    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 containing this error is marked with the aspx-script-editor-error icon. When a mouse pointer hovers over this icon, the text of the error is displayed.

Enable Logging

When the scripting security is disabled, you can use NLog for debug tracing by specifying the corresponding nlog configuration in the Web.config file of the worker service located at 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 the information about a current user in report scripts, use the ReportContext functionality. The following code illustrates how to display 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 illustrates how to access a report's data source in scripts by handling the DataSourceDemanded event of the report.

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 illustrates how to access and customize 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 illustrates how to access the settings of a multi-value data source parameter and select 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;
}
See Also