Use Scripts in Reports
In on-premises installations of , you can include C# scripts in your .
Scripting allows authors to write code that executes on your server. This can represent a security vulnerability if your authors are not trusted.
Scripting is by default. You can if you want.
To use scripts in , you must be familiar with C#.
Scripting is available in on-premises installations of only. Scripting is not available in cloud .
In , scripts are custom event handlers. When you configure a script, you associate the script with a particular element—a control, a band, or the itself—and a predefined event, such as Before Print or After Print. Each element in a has its own set of events. The event is fired relative to generation of the specified element. As the event handler, the script runs when the event is fired.
Example 1: If you associate the script with the Report Header band and After Print event, the script will run immediately after the Report Header is generated.
Example 2: If you associate the script with a particular control and the Before Print event, the script will run immediately before the control is generated.
The script runs as many times as the associated element is generated.
Example 3: If you associate the script with the Page Footer band, the script will run once per page, because the Page Footer is generated once per page.
Main Steps to Configure a Script
To add a script to a :
-
Open the in .
-
Click Scripts to open the Script Editor.
-
Specify when the script will run.
To do this, you need to associate the script with a element (a control, a band, or the itself) and an event. The event will be fired relative to generation of the specified element. The script is the event handler—it runs whenever the event is fired for the specified element.
Configuring the element and event is mandatory. If you skip this step, the script will not run.
-
Select the element to associate the script with.
Drop-down list in the Script Editor to associate the script with a element
-
Select the event.
Drop-down list in the Script Editor to select the event
The Script Editor creates a script stub. The script is named for the element and event that you selected.
Script stub created by the Script Editor
-
-
Type or paste the remainder of the script into the stub.
If you type the script, the Script Editor provides intelligent code completion as you type.
-
Click Validate to validate the script.
-
When the script is valid, click Designer to return to the design surface.
-
To test your script, preview the .
Example - Use a Script to Show Feature Attachments
This example uses a C# script to show feature attachment images in a report.
This is intended as an example only. If you want to use feature attachments in a report, use one of the feature attachment controls. See Configure Feature Attachment Controls for information.
-
Sign in to on-premises Report Designer.
-
If you have not yet configured the data source for a feature service that has feature attachment images, configure it now.
-
Create a report that uses the data source.
-
Add data fields and other controls to the report.
-
Drag a Picture Box control to the design surface, resize it, and configure its settings, except the image's location. The script will construct the image's URL.
Set the image's Sizing property to Zoom Image to enlarge or shrink the image to fit the control.
Example report layout
-
Click Scripts to open the script editor.
-
Select the report element to associate with the script, in this case, detailBand1. Alternatively, you could select the pictureBox1 control.
Your selection determines the list of events that you can choose from.
Location of drop-down list to select the report element
-
Select Before Print from the Select event drop-down list.
A script stub appears:
private void detailBand1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e) {
}Stub for a script that runs before detailBand1 is generated
-
Add the remaining code to the stub:
CopypictureBox1.ImageUrl = "[image-url-no-image]";
var serviceUrl = "[feature-layer-url]";
var fid = GetCurrentColumnValue("OBJECTID");
var attachmentsUrl = serviceUrl + fid + "/attachments?f=json";
using (var wc = new System.Net.WebClient())
{
using (var stream = wc.OpenRead(attachmentsUrl))
{
var serializer = new Newtonsoft.Json.JsonSerializer();
using (var sr = new System.IO.StreamReader(stream))
using (var jsonTextReader = new Newtonsoft.Json.JsonTextReader(sr))
{
var response = serializer.Deserialize<Newtonsoft.Json.Linq.JObject>(jsonTextReader);
var infos = response["attachmentInfos"] as Newtonsoft.Json.Linq.JArray;
if (infos != null)
{
var info = System.Linq.Enumerable.FirstOrDefault(infos, (x) =>
{
var contentType = (string)x["contentType"];
if (!string.IsNullOrEmpty(contentType) && contentType.StartsWith("image/"))
{
return true;
}
return false;
});
if (info != null)
{
var imageUrl = serviceUrl + fid + "/attachments/" + info["id"].ToString();
pictureBox1.ImageUrl = imageUrl;
pictureBox1.Visible = true;
}
}
}
}
} -
If the name of the Picture Box control is not
pictureBox1
, change all instances ofpictureBox1
to the correct name. -
Locate the following line and replace
[image-url-no-image]
with the URL of the image that you want to show in the report when a feature does not have an attached image.CopypictureBox1.ImageUrl = "[image-url-no-image]";
-
Locate the following line and replace
[feature-layer-url]
with the URL of the feature layer.Copyvar serviceUrl = "[feature-layer-url]";
The URL must end with a slash (
/
). -
If the layer's object ID field is not called
OBJECTID
, locate the following line and replaceOBJECTID
with the correct field name.Copyvar fid = GetCurrentColumnValue("OBJECTID");
-
Click Validate to validate the script.
-
When the script is valid, click Designer to return to the design surface.
-
Click Preview to preview the report.
Preview of a report showing feature attachments located by a C# script