ptg
481
Using Reporting Services Web Services
static string GetReportXML_VS2008(string ReportPath)
{
//creates a new web service (proxy) and sets its credentials
ReportExecutionServiceSoapClient rs = new ReportExecutionServiceSoapClient();
//Windows authentication
rs.ClientCredentials.Windows.AllowedImpersonationLevel =
System.Security.Principal.TokenImpersonationLevel.Impersonation;
// Setup Render() call
byte[] result = null;
string encoding, mimeType, extension, DeviceInfo=null;
Warning[] warnings = null;
string[] streamIDs = null;
try
{
string historyID = null;
ExecutionInfo ExecInfo;
ExecutionHeader ExecHeader;
ServerInfoHeader SvrInfoHeader;
//Note: set TrustedUserHeader = null, this is undocumented structure
ExecHeader = rs.LoadReport(null, ReportPath, historyID,
out SvrInfoHeader, out ExecInfo);
rs.Render(ExecHeader, null, “XML”, DeviceInfo, out result,
out extension, out mimeType, out encoding,
out warnings, out streamIDs);
//Gets a byte stream with Comma Separated Value (XML) layout
return System.Text.Encoding.ASCII.GetString(result);
}
catch (SoapException e)
</security>
Note that
Windows
in the preceding code sample is case sensitive.
You may see the following exception if security is improperly configured:
Message=”The HTTP request is unauthorized with client authentication scheme
‘Anonymous’. The authentication header received from the server was ‘Negotiate,NTLM’.”
You may see this exception if you do not have sufficient privileges to access:
An unhandled exception of type ‘System.Net.WebException’
occurred in system.web.services.dll. Additional information:
The request failed with HTTP status 401: Unauthorized.
The following is an exception if you do not request to impersonate a user:
Message=”The HTTP request is unauthorized with client authentication scheme ‘Ntlm’.
The authentication header received from the server was ‘NTLM’.”
Also notice:
<client>
<endpoint address=http://127.0.0.1:80/ReportServer/ReportExecution2005.asmx
...
This is an address of the
ReportExecution2005
services end. In the preceding example, it
is pointing to the
localhost
(127.0.0.1) port
80
. If you need to configure your applica-
tion to use another SSRS, you can just modify the endpoint address.
A call to
GetReportXML
_
warnings for which SSRS did not generate exceptions. In production code, you need to
make sure to incorporate handling for warnings.
The example uses part of the information available in
SoapException
.
SoapException
has
four properties:
. Actor: The code that caused the exception.
. Detail: The XML describing application-specific error information.
Detail
is an
XMLNode
object, and inner text from
Detail
can be accessed for the flow control (for
example,
if(ex.Detail[“ErrorCode”].InnerXml == “rsItemNotFound”) {/*handle
the error*/}
).
. HelpLink: A link to a Help file associated with the error.
. Messsage: A message describing the error.
The Report Execution web service is very sensitive to the report’s path, requires the path
to start from
/
(slash), and does not accept URL-encoded strings. If the path is incorrect,
the web service returns an
ItemNotFoundException
or
InvalidItemPathException
/Samples/My Demolist
(prefixed with slash when SSRS
is in the native mode and without URL encoding).
Actions in
GetReportXML_VS2008 ()
should produce the same result as
http://localhost/ReportServer/ReportExecution2005.asmx?/Samples/DemoList&rs:
Command=Render&rs:Format=XML
. The difference is that the web service call is not interac-
tive, but the web service call allows an application to receive and process a report’s XML
internally.
System.Text.Encoding.ASCII.GetString
is used to convert a
byte[]
array that
Render()
returns to a string. Note that ASCII is an option suitable for text-based formats, such as
XML and CSV. Other converters (such as Unicode) are available in the
System.Text.Encoding
namespace.
As a comparison, to use .NET 2.0 web services style code, you follow steps 1 through 4
above to access the Add Service Reference dialog box and then complete the following steps:
1. Access the Add Web Reference dialog box by clicking the Advanced button on the
Add Service Reference dialog box.
2. Enter
http://<server>/ReportServer/ReportExecution2005.asmx
in the URL field.
3. Click Go.
4. Set Web Reference Name to
ReportExecution2005_VS2005
FIGURE 28.3
Add Web Reference dialog box.
From the Library of STEPHEN EISEMAN
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
ptg
486
rs.LoadReport(ReportPath, null);
//Gets a byte stream with Comma Separated Value (XML) layout
result = rs.Render(“XML”, null, out extension, out encoding,
out mimeType, out warnings, out streamIDs);
return System.Text.Encoding.ASCII.GetString(result);
}
catch (SoapException e)
{
//Return exception message, if exception occurred
return e.Message;
}
}
The line
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
is very
important. An application must supply credentials to the SSRS web service before the
application can access a report.
DefaultCredentials
is the Windows authentication for
the user.
Report Management Web Service (ReportService
2005.asmx)
Previously in this chapter, you saw an example of the Report Execution web service
(
Report Management Web Service (
ReportService2005.asmx
)
28
3. Add a reference to the proxy in the code (using
EmbeddedReport.Report
Execution2005;
).
4. Call Report Management web service methods.
The following is an example of a console application returning items stored on SSRS start-
ing from the root
/
folder:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsoleApplication1.ReportService2005;
using System.Web.Services.Protocols;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//creates new Web service (proxy) and set its credentials
ReportingService2005 rs = new ReportingService2005();
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
try
{
, and
Unknown
items.
How to Script Reporting Services (Using the RS
Utility)
The RS utility is a scripting utility that enables access to Reporting Services functionality
using Visual Basic .NET scripts. In scripts, you can define classes and use other object-
oriented functionality of Visual Basic .NET.
By default, the RS utility is installed in the
C:\Program Files\Microsoft SQL
Server\100\Tools\Binn
directory. SSRS no longer comes with sample scripts, but you can
download samples from the online community www.codeplex.com/MSFTRSProdSamples/
Release/ProjectReleases.aspx?ReleaseId=16045. You can dissect and modify sample scripts to
fit scenarios at hand. Scripting is a convenient way to automate repetitive administrative
tasks or tasks that apply to a group of items.
Executing the
rs /?
command yields the following usage direction:
Microsoft (R) Reporting Services RS
Version 10.0.1600.22 ((SQL_PreRelease).080709-1414 ) x86
Executes script file contents against the specified Report Server.
RS -i inputfile -s serverURL [-u username] [-p password]
[-l timeout] [-b] [-e endpoint] [-v var=value] [-t]
-i inputfile Script file to execute
-s serverURL URL (including server and vroot) to execute
script against.
-u username User name used to log in to the server.
-p password Password used to log in to the server.
-e endpoint Web service endpoint to use with the script.
For Each Ext In Extensions
Select Ext.ExtensionType
Case ExtensionTypeEnum.Delivery
Type = “Delivery”
Case ExtensionTypeEnum.Render
Type = “Render “
Case ExtensionTypeEnum.Data
Type = “Data “
Case Else
Type = “Other “
End Select
Console.WriteLine(Type + “ “ + Ext.Name)
Next
End Sub
ReportServerVersionNumber
and
ReportServerEdition
are properties of Reporting
Services. You need to call Reporting Services before the properties are set. If you place this
call after you access Reporting Services properties, those properties will be empty. This is
what we are doing in the following line:
Extension()=rs.ListExtensions(ExtensionTypeEnum.All)
The scripting utility provides an internal reference to Reporting Services through the
rs
object, which is ready to be used in scripts without an explicit instance creation. The
command to execute the script might look like the following:
rs -iRsUtilTest.rss -shttp://localhost/ReportServer
Working with Report Parameters
Report parameters are encapsulated by two classes: The
ParameterValue
parameters[0] = new ParameterValue();
parameters[0].Name = “SalesOrderNumber”;
parameters[0].Value = “SO43659”;
rs.SetExecutionParameters(parameters, “en-us”);
result = rs.Render(format, devInfo, out extension,
out encoding, out mimeType,
out warnings, out streamIDs);
Or the following code for Visual Studio 2008, .NET 3.5, WCF style. Note that the main
differences between WCF and .NET 2.0 style, in this case, are in the
SetExecutionParameters
and
Render
function signatures:
ExecutionInfo ExecInfo;
ExecutionHeader ExecHeader;
ParameterValue[] parameters = new ParameterValue[1];
parameters[0] = new ParameterValue();
parameters[0].Name = “SalesOrderNumber”;
parameters[0].Value = “SO43659”;
rs.SetExecutionParameters(ExecHeader, null, parameters, “en-us”, out ExecInfo);
rs.Render(ExecHeader, null, “XML”, DeviceInfo, out result, out extension,
out mimeType, out encoding, out warnings, out streamIDs);
In the previous examples, you can see two out of three string properties of the
ParameterValue
class:
Name
and
Value
. The third property is a
Label
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
ptg
491
Security When Calling a Web Service (.NET 2.0 Style)
28
As you can probably tell, the
ReportParameter
class properties directly correspond to the
report parameter properties that were set during the design phase. See Chapter 12, “Report
Parameters.”
Security When Calling a Web Service (.NET 2.0 Style)
Out of the box, SSRS supports Windows authentication and authorization. If you need to
have custom authentication, SSRS provides this through custom authentication (or secu-
rity) extensions. You have to develop a new security extension to handle custom authenti-
cation.
.NET Framework greatly simplifies Windows and basic authentication handling through
classes in the
System.Net
namespace.
Before deciding which authentication method to use, consider security implications of
each type of authentication: anonymous, basic, and integrated/Windows.
As you might recall, we leveraged the .NET Framework to set Windows credentials in the
GetReportXML2005()
method earlier in this chapter:
TABLE 28.2
Commonly Used Public Properties of the ReportParameter Class
Name Description
AllowBlank
Indicates whether an empty string is a valid value for the para-
meter
From the Library of STEPHEN EISEMAN
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
ptg
492
CHAPTER 28 Using Reporting Services Web Services
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
To pass basic authentication credentials, you can substitute the preceding code with the
following code:
rs.Credentials = new System.Net.NetworkCredentials(“user name”,
“password”, “domain”);
The credentials must be set before the use of any methods in the SSRS web service. Calls
to a web service method before setting credentials receive an error:
HTTP 401 Error:
Access Denied
.
Security When Calling a Web Service (.NET 3.x,
WCF Style)
A detailed WCF security discussion is beyond scope of this chapter, but we do want to
present you with some basic concepts to help you understand what we have done in our
WCF example. You can find a detailed WCF security review at http://msdn.microsoft.com/
en-us/library/ms732362.aspx and a pretty good short explanation at www.code-
magazine.com/articleprint.aspx?quickid=0611051.
When we added the
ReportExecution2005
service reference, Visual Studio 2008 added a
service reference and
<system.serviceModel>
configuration entries in the
app.config
file.
28
<message clientCredentialType=”UserName” algorithmSuite=”Default” />
</security>
A complete set of options for
basicHttpBinding
can be expressed by the following config-
uration (curved brackets indicate that one of the options can be selected):
<security mode=
{“None” | “Transport” | “Message”
| “TransportWithMessageCredential” | “TransportCredentialOnly”}>
<transport clientCredentialType= {“None” | “Basic”
| “Digest” | “Ntlm” | “Windows”}
proxyCredentialType={“None” | “Basic”
| “Digest” | “Ntlm” | “Windows”}
realm=”” />
<message
algorithmSuite=
{
“Basic128” | “Basic192” | “Basic256” | “Basic128Rsa15” |
“Basic256Rsa15” | “TripleDes” | “TripleDesRsa15” |
“Basic128Sha256” | “Basic192Sha256” | “TripleDesSha256” |
“Basic128Sha256Rsa15” | “Basic192Sha256Rsa15” |
“Basic256Sha256Rsa15” | “TripleDesSha256Rsa15”
}
clientCredentialType=
{“Certificate” | “IssuedToken” | “None” |
“UserName” | “Windows”
}
/>
</security>
client using the service’s SSL certificate. The service must be configured with
the SSL certificate. Client authentication is provided by means of SOAP
message security. This mode is applicable when the user is authenticating with
a
UserName
or
Certificate
credential, and there is existing HTTPS for securing
message transfer.
. TransportCredentialOnly: We use this method in our example. It is,
perhaps, the easiest one to use, but use this mode with caution. This mode does
not provide message integrity or confidentiality; it provides only HTTP-based
client authentication. You can use it in environments where other methods
(such as IPSec) provide the transfer security and client authentication is
provided only by the WCF infrastructure.
. transport: Configuration for
Transport
mode of security, including
TransportWithMessageCredential
and
TransportCredentialOnly
.
. clientCredentialType: Specifies the type of credential to be used when perform-
ing HTTP client authentication. Possible options for this configuration are as follows:
. None: Anonymous authentication
. Basic: Basic authentication
. Digest: Digest authentication
. Ntlm: Client authentication using NTLM
. Windows: Client authentication using Windows
. Certificate: Client authentication using a certificate
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
ptg
495
Using SSL to Increase Security
28
And Windows credentials were also reflected in the configuration as follows:
<security mode=”TransportCredentialOnly”>
<transport clientCredentialType=”Windows” proxyCredentialType=”None”
Using SSL to Increase Security
To increase security, an administrator can use
SecureConnectionLevel
to configure SSRS
and enforce web service client applications to leverage various levels of SSL communica-
tions for Report Server web service method calls. (This configuration also affects Report
Manager.)
SSRS uses
SecureConnectionLevel
(located in
RSReportServer.config
) to determine which
web service methods require an SSL connection. The default is
0
(noted in the configura-
tion as
<Add Key=”SecureConnectionLevel” Value=”0” />
).
SecureConnectionLevel
has
four levels that affect URL and SOAP interfaces that SSRS exposes:
. 0: SSRS does not check for secure connections (SSL). Method calls can still be made
All snippets require a proper SSRS endpoint reference as described earlier in the chapter,
web and SOAP proxy references, and the following calls prior to calling any of the
methods:
ReportingService2005 rs = new ReportingService2005();
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
Although the examples use VS2005 .NET 2.0 style, you can still use the style in Visual
Studio 2008 as we have discussed earlier in this chapter, or you can modify the samples to
use VS2008 .NET 3.5 WCF style.
. Find and cancel all jobs:
Job[] jobs = null;
jobs = rs.ListJobs(); //Get a list of current jobs.
foreach (Job job in jobs)
{
if (job.Status == JobStatusEnum.Running || job.Status ==
JobStatusEnum.New)
{
rs.CancelJob(job.JobID);
}
}
. Create a folder item:
rs.CreateFolder(strFolderName, strParentFolderName, null);
. Create a report item:
FileStream stream = File.OpenRead(“sample.rdl”);
Byte[] rdl = new Byte[stream.Length];
stream.Read(rdl, 0, (int) stream.Length);
stream.Close();
rs.CreateReport(strReportName, strFolderName, false, rdl, null);
. Delete an item:
rs.DeleteItem(strItemPath)
. Get an RDL of a report from SSRS:
utility for
scripting of the SSRS web service access using Visual Basic .NET scripts. The
rs.exe
utility
is frequently used to automate high-volume management tasks: report deployment, secu-
rity management, and so on.
From the Library of STEPHEN EISEMAN
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
ptg
This page intentionally left blank
From the Library of STEPHEN EISEMAN
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
ptg
CHAPTER
29
Extending Reporting
Services
IN THIS CHAPTER
. Common Considerations for
Custom Reporting Services
Extensions: Implementation,
Deployment, and Security
. Report Definition
Customization Extension
. Delivery Extension
. Interactions Between User,
SSRS, and a Delivery Extension
. Custom Report Items
S
SRS is designed for extensibility. In the previous chapters,
used by each renderer to show the report. It gives
From the Library of STEPHEN EISEMAN
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
ptg
500
CHAPTER 29 Extending Reporting Services
consistency across rendering format. The object model for RPL is not publicly
exposed in the 2008 release.)
. Other:
SemanticQuery
and
ModelGeneration
to extend Report Builder’s functional-
ity.
EventProcessing
to act on the events generated by Report Server.
. Security: By default, SSRS uses Windows integrated authentication.
A complete list of extensions installed on a particular instance of SSRS can be retrieved by
calling
ReportingService2005.ListExtensions
(
ExtensionTypeEnum.All
) or by examining
the
rsreportserver.config
file.
Rendering is, perhaps, the most developed category of extensions. With a wide range of
rendering extensions and a multitude of applications (including Microsoft Office) that
“understand” HTML, it is hard to think of a new rendering extension that would be
immediately useful.
From the Library of STEPHEN EISEMAN
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.