Академический Документы
Профессиональный Документы
Культура Документы
Programming Reporting
Services
Contents
Overview 1
Lesson: Supporting User Access 2
Demonstration 1: Querying for Server
Information 7
Demonstration 2: Rendering Reports via URL
Access 13
Demonstration 3: Rendering Reports via Web
Service 15
Lesson: Automating Report Management 18
Demonstration 4: Automating Report
Management 20
Lesson: Advanced Programmability 25
Demonstration 5: Creating a Custom
Assembly 31
Information in this document, including URL and other Internet Web site references, is subject to
change without notice. Unless otherwise noted, the example companies, organizations, products,
domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious,
and no association with any real company, organization, product, domain name, e-mail address,
logo, person, place or event is intended or should be inferred. Complying with all applicable
copyright laws is the responsibility of the user. Without limiting the rights under copyright, no
part of this document may be reproduced, stored in or introduced into a retrieval system, or
transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or
otherwise), or for any purpose, without the express written permission of Microsoft Corporation.
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual
property rights covering subject matter in this document. Except as expressly provided in any
written license agreement from Microsoft, the furnishing of this document does not give you any
license to these patents, trademarks, copyrights, or other intellectual property.
Microsoft, MS-DOS, Windows, Windows NT, ActiveX, BackOffice, FoxPro, JScript, MS-DOS,
PivotChart, PivotTable, PowerPoint, Visual Basic, Visual C++, and Visual Studio are either
registered trademarks or trademarks of Microsoft Corporation in the United States and/or other
countries.
The names of actual companies and products mentioned herein may be the trademarks of their
respective owners.
Overview
Microsoft Confidential
This module will introduce you to the basic concepts of controlling Reporting
Services programmatically.
By the end of this module you will be able to do the following:
Create a simple Visual Basic .NET program to do the following:
• Retrieve information about published reports and folders from the server
• Render a specific report to a selected format
• Publish a report to the server
• Delete a report from the server
Understand how reporting services can be extended
Create a custom assembly that includes a function you can call from an
expression in a report
Microsoft Confidential
In this lesson you will learn the basics for creating a custom interfaces or report
browser for end users. Creating a custom browser typically requires two separate
tasks: first, discovering information from the server, such as which reports are
available for the current user; and second, actually rendering the report. You will
create a very simple application that accomplishes both these tasks.
Microsoft Confidential
All of these standards are set by the World Wide Web Consortium (www.w3c.org).
The World Wide Web Consortium is an outside organization composed of around
450 members.
Report Design
Browsing
Report Browser
Information
Delivery Channels
ManagementOutput Formats
Report Manager
Information Data Flow
Control Flow
Microsoft Confidential
Discovering server information is useful for two separate purposes. On the one hand
it is often necessary when creating a custom browser for end users. On the other
hand, it can be useful when creating a custom management tool.
One of the major reasons for retrieving information about items on the server is to
facilitate user access to appropriate reports. You can programmatically find which
reports are available for a particular user, whether those reports require parameters,
and other pertinent information about the reports.
In addition, you can also discover report information as part of an application to
manage reports. Before changing attributes of a report, your application may want to
know what the current attributes are.
The primary mechanism for getting information about Reporting Services items is
by communicating with the Web Service, which is accessible via standard SOAP
calls. Fortunately, the .NET infrastructure makes it easy to write applications that
communicate with a Web Service.
Microsoft Confidential
The first step in communicating programmatically with the Report Server Web
Service is to create a Web Reference to the service. The Web Reference translates
normal object methods and properties into the appropriate SOAP calls. (It is also
possible to create an explicit proxy class for translating to and from SOAP, but the
Web Reference automatically creates the necessary class for you.) Visual Studio
walks you through the process of creating a Web Reference. The URL that contains
the definition of the web service is
http://«server»/reportserver/reportservice.asmx?wsdl.
Once you create a reference to the primary class—RSWebService—you must first
establish credentials by passing an item from the local .NET Credentials Cache.
When you retrieve information about reporting items, you are not limited to reports.
You also retrieve information about other items on the server, most notably folders
that organize reports.
There are two primary mechanisms for finding information about reporting items,
ListChildren—which allows you to navigate through the folder hierarchy—and
FindItems—which allows you to filter all or part of the hierarchy by search
conditions. When you create a search condition, you make comparison to certain key
attributes of the reporting items, such as the name or create date or modify date.
When you search for items, the list returned to you is automatically filtered by the
authenticated user’s permissions.
Ç Launch Visual Studio and create a new Visual Basic Windows Form
1. Click Start, point to All Programs, point to Microsoft Visual Studio .NET
2003, and then click Microsoft Visual Studio .NET 2003.
2. On the File menu, point to New, and then click Project.
3. In the New Project dialog box, in the Project Types pane, click Visual Basic
Projects.
4. In the Templates list, click the Windows Application icon.
5. In the Name textbox, replace the default name with Web Service, in the
Location textbox, type C:\Reporting Services Training\Workspace\ and then
click OK.
The project contains a blank windows form, Form1.vb.
6. Resize the form to fill most of the available space.
7. On the Toolbox, double-click the TextBox control, the Button control, and the
ComboBox control.
Tip: By default, the Toolbox is located on the left side of the screen. If the
Toolbox is not visible, on the View menu, click Toolbox.
8. Arrange the Button to the right of the TextBox, and the ComboBox below them
both.
9. Select the TextBox control, and in the Properties window, clear the Text
property value.
10. Select the Button control, in the Properties window, in the Text property,
delete the default value, and then type Get Items
11. Select the ComboBox control, and in the Properties window, for the
DropDownStyle property, select Simple.
Tip: If the Solution Explorer pane is not visible, on the View menu, click
Solution Explorer.
4. After the web service is located, in the Web reference name textbox, delete the
default value, type ReportService and then click Add Reference.
Ç Add code to the Find Items button to search for matching items
1. On the form double-click the Get Items button.
This opens the Visual Basic page that controls the form, and adds a new
procedure called Button1_Click. This code will run whenever you click the
Find Items button. The code to enter in this procedure is in a text file.
2. Click Start, point to All Programs, point to Accessories, and then click
Notepad.
3. In Notepad, on the File menu, click Open.
4. Navigate to the C:\Reporting Services Training\M07\Demo\Step1.txt and then
click Open.
This file contains the following code:
Dim myReportService As New ReportService.RSWebService
Dim myCatalogItems As ReportService.CatalogItem()
myReportService.Credentials = _
System.Net.CredentialCache.DefaultCredentials
Dim mySearchConditions(0) As
ReportService.SearchCondition
mySearchConditions(0) = New
ReportService.SearchCondition
mySearchConditions(0).Name = "Name"
mySearchConditions(0).Value = TextBox1.Text
myCatalogItems = myReportService.FindItems("/", _
Nothing, mySearchConditions)
Dim i As Integer
ComboBox1.Items.Clear()
For i = 0 To myCatalogItems.Length - 1
ComboBox1.Items.Add(myCatalogItems(i).Name)
Next i
The first two lines respectively create a connection to the web service and
prepare a place to store the list of returned items. These two lines will go at
the top of the page.
The first indented statement (split onto two lines for readability), tells the
service to use the credentials of the currently logged in user. This line needs
to execute once when you first start the program.
The remaining statements execute each time you click the Find Items
button.
The next block of statements creates a list consisting of one search
condition. This search condition tests the “Name” value of a report item
with the text from the Textbox.
The next statement uses the FindItems command to tell the report service to
“find” the “items” that match the list of conditions, and put the information
about the items into the myCatalogItems list.
The final block of statements puts the name of each item in the retrieved list
into the combo box, after first clearing the box of any previous items.
Last Saved: 9/30/2003 9:19:00 PM
Last Printed: 9/30/2003 9:58:00 PM
Module 07: Programming Reporting Services 9
5. Copy the first two lines and switch to Visual Studio, after the Inherits
statement, press ENTER and then paste the two copied lines.
6. Switch back to Notepad and copy the first indented statement (the credentials
statement).
7. Switch to Visual Studio, click the plus sign next to Windows Form Designer
generated code.
8. After the InitializeComponents() statement, press ENTER, and then paste the
copied line.
9. Click the minus sign next to #Region " Windows Form Designer generated
code "
10. Switch back to Notepad, copy the remaining lines, and on the File menu, click
Exit to close Notepad.
11. In Visual Studio, before the End Sub statement, paste the copied lines.
Your form should now look like the following image:
12. On the Debug menu, click Start to run the form and wait for Form1 to appear.
13. In Form1, with the textbox empty, click the Get Items button and wait for the
names to appear in the combo box.
The names of all the items on the server—both folders and reports—appear.
14. In the textbox, type Sales, and click the Get Items button.
The names of all items that contain the word Sales appear.
15. Click the Close button to close the form.
Note Leave this Visual Basic project open, as you will continue the project in
Demonstration 4.
Rendering Reports
Report Design
Report Browser
Data Flow
Control Flow
Microsoft Confidential
The second part of facilitation user access to reports is to actually render the reports.
Microsoft Confidential
Microsoft Confidential
Using a URL to render a report is clearly the simplest way to render a report, but
there are times when you programmatically want more control. You can issue a
Render command to the web service directly and programmatically, in essentially
the same way that you get report item information from the service: create a Web
Reference, pass authentication, and then execute the Render method.
The Render method has many parameters, including the full path for the report and
the rendering type (when you call the Render method to create HTML output, you
must specify the HTML level you want). You also pass as arguments any report
parameters or other options. Because the proxy is translating the request to SOAP to
send to the service, you must include the keyword Nothing for any optional
arguments you choose not to use.
When you call the Render method, the report is not automatically rendered in the
browser, which means that you can call the method from any application, not just a
browser. The function returns the rendered report in the form of a byte array. You
then programmatically do with that byte array whatever you want: for example, you
can display an HTML rendered report in a literal Web Control, or you can save any
report output to a file. If you want to dump the report results to a list in a
spreadsheet—for sorting and other analysis—you may want to retrieve the data as
comma separated values (CSV), which you can then copy into Excel.
1. Click Start, point to All Programs, point to Microsoft Visual Studio .NET
2003, and then click Microsoft Visual Studio .NET 2003.
2. On the File menu, point to Open, and then click Project.
3. In the Open Project dialog box, navigate to the C:\Reporting Services
Training\M07\Demo\Answers\Demo 02\ folder containing Demonstration 2
answer files.
4. Click the Web Service.sln file and then click Open.
5. In Solution Explorer, double-click the Form1.vb form.
Tip: If the Solution Explorer pane is not visible, on the View menu, click
Solution Explorer.
Tip: By default, the Toolbox is located on the left side of the screen. If the
Toolbox is not visible, on the View menu, click Toolbox.
You may need to scroll down the list of controls to see the RichTextBox control.
2. Position and resize the control to take up all available space on the right side of
the form.
3. With the RichTextBox control selected, in the Properties window, clear the
Text property value.
Ç Render a report when you change the selection in the list of items
1. On the form, double-click the ComboBox control.
This displays the code page with a new procedure named
ComboBox1_SelectedIndexChanged. This code will run each time you
select a new item in the Combo Box.
2. Click Start, point to All Programs, point to Accessories, and then click
Notepad.
3. In Notepad, on the File menu, click Open.
4. Navigate to the C:\Reporting Services Training\M07\Demo\Step2.txt and then
click Open.
This file contains the code necessary for rendering the report into the
RichTextBox.
Dim myDefinitionBytes As Byte()
Dim myEncoding As System.Text.Encoding = _
System.Text.Encoding.Unicode
Try
myDefinitionBytes = myReportService.Render( _
myCatalogItems(ComboBox1.SelectedIndex).Path,
"CSV", _
Nothing, Nothing, Nothing, Nothing, Nothing, _
Nothing, Nothing, Nothing, Nothing, Nothing)
RichTextBox1.Text = _
myEncoding.GetString(myDefinitionBytes)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
The first statement creates a buffer—called a byte array—to store the
details for the report. You retrieve a rendered report into a byte array
regardless of the rendering type you use.
The second statement creates an Encoding object that allows you to
translate the byte array, which is stored in Unicode text (capable of
handling exotic text such as Chinese) into a simple string that you can put
into the RichTextBox.
The Try, Catch, and End Try statements allow you to display an error
message if the report does not render properly. For example, you will see an
error if you select a folder from the list, rather than a report.
The first statement in the Try section renders the report into the byte array.
This Render command is passed to the server via the Web Reference that
you created earlier. There are a lot of possible arguments to the Render
command, and if you don’t need them, you must enter Nothing as a
placeholder. Notice the “CSV” argument. This is what tells the Render
command to create CSV output. If you use “HTML3.2” in the command,
the report will come out with HTML formatting. You pass to the report
server the complete path of the report you want to render. The path is one of
the properties stored in the catalog list created by the FindItems command.
This command gets the item from the list corresponding to the item selected
in the combo box.
Different report formats require different encoding. So, for the “CSV”
format presented here use Unicode. To render a report in “HTML”, change
the encoding to UTF8.
The second (and final) statement in the Try section uses the Encoding
object to translate the byte array into a string and put it into the Rich Text
Box control.
The Catch portion of the code displays an error message if something goes
wrong with either Rendering the report, or translating the byte array.
5. In Notepad, select all by using Ctrl + A and then copy by using Ctrl + C.
6. On the File menu, click Exit to close Notepad.
7. In the Visual Basic code, paste all the statements before the End Sub statement
of the ComboBox1_SelectedItemChanged procedure.
Report Browser
In this lesson, you will learn about how to write your own custom tools for
managing reports on the server, so that you could replace the functionality of the
Report Manager application You will write a very simply application that publishes
a report and deletes a report.
When you automate report management via the web service, you can do anything
that you can do in the Report Manager application. That’s because the Report
Manager application uses the web service to carry out its tasks. You can write
custom code to add or delete reports, administer security or schedules, or any of a
large number of other tasks.
Any report management task that you must execute multiple times becomes a
candidate for a custom application. If you are running a report server farm, for
example, you may want to apply identical settings to multiple servers. Or you may
want to apply the same security or subscription settings to multiple reports. Or create
an application that generates report RDL files and then automatically deploys the
generated reports.
As with other web service applications, you must create a Web Reference and pass
credentials in order to communicate with the server. The RSWebService class then
has methods and properties for manipulating reports.
1. Click Start, point to All Programs, point to Microsoft Visual Studio .NET
2003, and then click Microsoft Visual Studio .NET 2003.
2. On the File menu, click Open Solution.
3. In the Open Solution dialog box, navigate to the C:\Reporting Services
Training\M07\Demo\Answers\Demo 04\ folder containing Demonstration 4
answer files.
4. Click the Web Services.sln file and then click Open.
5. In Solution Explorer, double-click the Form1.vb form.
Tip: If the Solution Explorer pane is not visible, on the View menu, click
Solution Explorer.
Tip: By default, the Toolbox is located on the left side of the screen. If the
Toolbox is not visible, on the View menu, click Toolbox.
Note You may need to resize the button to view all of the Delete Report text.
Important If you prefer not to copy and rename the report, you can edit the
location of the report in the code file in the next step.
fr.Read(myDefinitionBytes, 0, fr.Length)
myReportService.CreateReport("New Report", "/", False, _
myDefinitionBytes, Nothing)
Button1_Click(Nothing, Nothing)
In the first block, the first statement creates a place to store the report
definition, a byte array. The second statement creates an object capable of
reading a file, a FileStream object. It also sets up the object to read the
sample report file. The third statement prepares the byte array for the
number of characters that will be coming from the file.
In the second block, the first statement reads the characters from the file
into the byte array. The second statement passes that array to the Web
Service, giving it a name (New Report) and a location (/—the root folder).
The final block simply “clicks” the Get Items button so that the list will
refresh.
5. In Notepad, select all by using Ctrl + A and then copy by using Ctrl + C, and
then switch to Visual Studio.
6. In the Visual Basic code, paste all the statements before the End Sub statement
of the Button2_Click procedure.
Button1_Click(Nothing, Nothing)
The first statement deletes the selected item. To delete an item, you simply
pass its complete path. The path is stored in the collection of catalog
information retrieved by FindItems. In this case, you pass the path from the
item with the same number as the report currently selected in the combo
box.
The second statement “clicks” the Get Items button to refresh the list.
4. In Notepad, copy all the code, and then on the File menu, click Exit to close
Notepad.
5. In the Visual Basic code, paste all the statements before the End Sub statement
of the Button3_Click procedure.
Rather than write a complete executable application to carry out a management task,
you can create a script file and then utilize the RS.EXE application to execute the
script. The RS.EXE application manages the SOAP interface and the connection to
the web service. It also manages the necessary authentication.
In essence, a script file is the core of a Visual Basic application. It must have a
function named Main, but it doesn’t need to create a proxy or connect to the server
or pass authentication. You can create multiple script files—with the extension rss—
and then execute them in various batch process as command line arguments to
RS.EXE.
The demonstrations in this module are intended to give you the simplest possible
code that can carry out the essential requirements. The samples that come with
Reporting Services include more extensive applications that you can learn from. To
learn more about communicating with the web service and using URLs, check out
the RS Explorer application. In addition to basic navigation and rendering code, this
sample application includes a URL Generator class that is more powerful than
needed for the RS Explorer itself. This is a class that you can incorporate in your
own applications that need to generate URL string with parameters and other
sophisticated options.
If you want to create scripts for managing reports, you should look at the
PublishSampleReports sample script. This script shows how to create a shared data
source, upload multiple reports, and pass a command-line argument via the RS.EXE
application.
Server Configuration
Types of Extensions
Custom Extensions
Custom Assemblies
Microsoft Confidential
In this lesson you will learn about some advanced programmability options available
in Reporting Services. In addition to day-to-day operations such as creating or
customizing reports, facilitating user access to reports, and managing reports, there
are also some specialized ways in which you can automate Reporting Services,
particularly to control server configuration and to add extensions to the server.
Server Configuration
Microsoft Confidential
Normal report management is all done through SOAP calls to the Report Server
Web Service. In order for the server to respond to the SOAP calls, the service must
be running. There are, however, certain server configuration tasks that you may
want to be able to automate whether or not the server is actually running. In
particular, options that allow you to configure the server in the first place must run
before starting the service. For these configuration activities, Reporting Service
supports a Windows Management Interface (API). This interface does not replace
the normal Web Service activities, but provides a narrowly focused set of tools for
those tasks that must be executable without requiring the service to be running.
Types of Extensions
Report Design
Report Browser
Rendering
Extensions
Data Processing
Data Report Server
Extensions
Sources
Delivery
Output Formats Delivery Channels
Extensions
Report Manager
Security
Extensions Data Flow
Control Flow
Microsoft Confidential
Reporting Services was designed as an extensible architecture. All the major tasks of
Reporting Service are implemented as extensions. For example, rendering a report
as HTML is implemented as an HTML rendering extension. What this means is that
you can, if necessary, add additional customized extensions that integrate seamlessly
with the native functionality of the Report Server. Specifically, extensions come in
four flavors: Data Processing, Rendering, Security, and Delivery.
Custom Extensions
Microsoft Confidential
Microsoft Confidential
Microsoft Confidential
Creating and using a custom assembly from a report requires several steps. Some of
these steps—such as writing the function code—must clearly be done for each
function you create. Other steps—such as enabling custom assemblies in reports—
need to be done only once.
• You must create a new .NET class library and add functions to that library.
• You must create a strong key to secure the assembly so that you can add it to
the .NET Global Assembly Cache (GAC), from which it is accessible to your
reporting services project.
• You must edit two configuration files—one for the Report Designer, and one
for the Report Server—to allow custom assemblies to be used.
• You must add a reference within the report to any assemblies you wish to
use.
• Within any expression, you can then call the functions from the registered
assembly.
The function takes an input number and returns a color based on that
information.
4. In Notepad, select all by using Ctrl + A and then copy by using Ctrl + C, and
then close the file.
End Class
End Class
You should now have a new file in the root of the project called
AdventureWorks.snk. This file contains a public/private key pair that will
allow you to sign the assembly.
4. Keep the command prompt window open.
5. Click after the version number line, press <ENTER>, and then type the
following metadata tag to specify the key file:
<Assembly:
AssemblyKeyFile("../../AdventureWorks.Extensions.snk")>
For security purposes, Reporting Services does not enable custom assemblies in a
report until you explicitly enable them. To enable custom assemblies, you simply
delete comment marks from two configuration files.
1. In Windows Explorer, navigate to the folder C:\Program Files\Microsoft SQL
Server\80\Tools\Report Designer.
2. Make a copy of the RSReportDesigner.config file as a safety precaution.
3. Right-click the file RSReportDesigner.config, point at Open With, and click
Choose Program. Then select Notepad, and click OK.
4. Click Edit, and click Find. Type Comment, and click OK. Delete the comment
indicator before the <CustomAssemblies> tag, and the closing comment
indicator after the </CustomAssemblies> tag. Then save the file and close
Notepad.
5. Navigate to the folder C:\Program Files\Microsoft SQL
Server\MSSQL\Reporting Services\ReportServer.
6. Make a copy of the RSReportServer.config file as a safety precaution.
7. Right-click the file RSReportServer.config, point at Open With, and click
Choose Program. Then select Notepad, and click OK.
8. Click Edit, and click Find. Type Comment, and click OK. Delete the comment
indicator before the <CustomAssemblies> tag, and the closing comment
indicator after the </CustomAssemblies> tag. Then save the file and close
Notepad.
Tip: If the Solution Explorer pane is not visible, on the View menu, click
Solution Explorer.
4. In the Edit References dialog box, click the button in the upper right hand
corner to add a reference.
5. In the Add Reference dialog box, click the Browse button to navigate to the
custom assembly.
6. In the Select Component dialog box, navigate to the C:\Reporting Services
Training\Workspace\AdventureWorks.Extensions\bin folder.
7. Select the AdventureWorks.Extensions.dll file and then click Open.
8. Click OK twice.
5. In the Find dialog box, click Find Next. Keep the Find dialog box open.
6. Again replace IIF with
AdventureWorks.Extensions.CommonFunctions.GetColor and delete
everything between the closing parentheses after Value and the final closing
parentheses. The finished element should look like this:
<Color>=AdventureWorks.Extensions.CommonFunctions.
GetColor(Me.Value)</Color>
10. Activate the Design tab for the report by clicking the Product Profitability.rdl
[Design] tab, and then click the Preview tab.
Now some of the locations are yellow, because they are below the 15% cut-
off value for green, but are above the 10% cut-off value for red.