Back to Devexpress

Scripts

reportserver-115950-configuration-and-api-scripts.md

latest7.8 KB
Original Source

Scripts

  • Oct 13, 2023
  • 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.

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 ( ) located on the Report Designer’s toolbar.

  2. In the invoked Script Editor, specify the report control and an available event for this control.

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

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).

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

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

csharp
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.");
}
vb
Shared ReadOnly logger As NLog.Logger = NLog.LogManager.GetCurrentClassLogger()

Private Sub Report_BeforePrint(sender As Object, e As System.Drawing.Printing.PrintEventArgs)
    logger.Info("This is a test.")
End Sub

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.

csharp
private void label1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e) {
    if(ReportContext.Current != null) {
        ((XRLabel)sender).Text = ReportContext.Current.UserEmail;
    }
}
vb
Private Sub label1_BeforePrint(ByVal sender As Object, _ 
ByVal e As System.Drawing.Printing.PrintEventArgs)
    If ReportContext.Current IsNot Nothing Then
        CType(sender, XRLabel).Text = ReportContext.Current.UserEmail
    End If
End Sub

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.

csharp
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;
}
vb
Private Sub XtraReport_DataSourceDemanded(sender As Object, e As EventArgs)
    Dim report = TryCast(sender, XtraReport)
    Dim dataSource = TryCast(report.DataSource, 
        DevExpress.ReportServer.Infrastructure.Data.ReportServerDataSource)
    dataSource.Filters("Products") = "[CategoryID] = " + report.Parameters("CategoryId").Value
End Sub

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

csharp
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);
    }
}
vb
Imports System.Linq

Private Sub XtraReport_DataSourceDemanded(sender As Object, e As EventArgs)
    Dim report = TryCast(sender, XtraReport)
    Dim dataSource = TryCast(report.DataSource, 
        DevExpress.ReportServer.Infrastructure.Data.ReportServerDataSource)
    Dim Beginning_Date = dataSource.Parameters("Sales by Year").First(Function(p) 
        p.Name.Equals("@Beginning_Date"))
    Dim Ending_Date = dataSource.Parameters("Sales by Year").First(Function(p) 
        p.Name.Equals("@Ending_Date"))
    If report.Parameters(0).Value.ToString() = "Today" Then
        Dim now = DateTime.Now
        Beginning_Date.Value = New DateTime(now.Year, now.Month, now.Day)
        Ending_Date.Value = DirectCast(Beginning_Date.Value, DateTime).AddDays(1).AddTicks(-1)
    End If
End Sub

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

csharp
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;
}
vb
Imports System.Linq
Imports DevExpress.XtraReports.Parameters
Imports DevExpress.Data.Browsing
Imports DevExpress.ReportServer.Infrastructure.Data

Private Sub XtraReport_ParametersRequestBeforeShow(sender As Object, e As ParametersRequestEventArgs)
    Dim report = TryCast(sender, XtraReport)
    Dim parameter = report.Parameters("CategoryNames")
    Dim paramSource = TryCast(DirectCast(parameter.LookUpSettings, DynamicListLookUpSettings).DataSource, 
        ReportServerDataSource)
    DirectCast(paramSource, IListAdapter).FillList(Nothing)
    Dim dataContext = TryCast(DirectCast(report, IServiceProvider).GetService(GetType(DataContext)), DataContext)
    Dim values = LookUpHelper.GetLookUpValues(parameter.LookUpSettings, dataContext).[Select]
        (Function(x) DirectCast(x.Value, String)).ToArray()
    parameter.Value = values
End Sub