Understanding Reporting Services Web Service Access
 
In the last article titled, Accessing Reports via URLs and Embedding Reports, we saw the URL method of accessing reports. This method of access is suitable for ad-hoc access, given that you know the report server URL, the report that you want to access and the parameters that the report requires. It is also useful for embedding it inside applications to quickly get at the report. But what if you want more control?? Say for example you want to change the data source of the report at runtime or you want to create a new schedule for the report?? This type of control is not provided by the URL method. Fortunately, the report server also presents a full-fledged web service that allows you to do anything with the report server that the report manager can. Remember that the report manager is the .NET web based application that you can use to manage and maintain your reports. All that you can do with the report manager application, you can do using the reporting services web-service.

In this article we will see how to access the report server using the web-service and then see an example of how to use the web-service in a normal application. I will assume that you are already familiar with using SQL Reporting Services. If you are not, please read the following articles (in the same order):

I will also assume that you have some experience programming with web services. So let's get cracking!

The first step in programming against the web service is to make a reference to it. Before we the steps to do that, let us see how you can get at the WSDL for the reporting web service. You can find the web-service WSDL at the following location:


http://localhost/reportserver/reportservice.asmx

You can type this URL in your browser and observe the WSDL that is generated. Note that in the example above, the report server is running locally on my machine. The name of the web-service is called reportservice.asmx. If you can see the WSDL, then everything is fine and we can now proceed to build the sample.

The application that we will build here is a simple Windows Forms application. Open Visual Studio .NET and create an empty Windows Forms application. Then, add a reference to the reporting service web-service by using the URL shown above. The following figure shows the web-reference dialog box, with the reporting web-service referenced.

Note that we have given the web-reference a name of ReportWS. This name will be used in the program to reference the web service. Now that we have a reference to the web service, design a form as shown in the following figure.

The user interface is very simple. We have a button called Get All Items, which we will use to get all the items defined in the report server and a list box called lstFolders that will contain all the items. Let us now write the code for populating the list box with content. The following snippet illustrates the code to be written.


Public Sub New()
    MyBase.New()

    'This call is required by the Windows Form Designer.
    InitializeComponent()

    'Add any initialization after the InitializeComponent() call
    rs = New ReportWS.ReportingService
    rs.Credentials = System.Net.CredentialCache.DefaultCredentials
End Sub

Private Sub cmdGetItems_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGetItems.Click
    ' Create an array of catalog items to hold the results
    Dim reportItems As ReportWS.CatalogItem()
    ' Create a catalog item to iterate through the results
    Dim reportItem As ReportWS.CatalogItem
    Dim strDisplay As String

    ' The ListChildren method returns all the the child items given a path
    ' The second parameter indicates if we need to recurse through the sub-folders
    reportItems = rs.ListChildren("/", True)
    ' Iterate through the results
    For Each reportItem In reportItems
        ' Form the string to display and add it to the list box
        strDisplay = reportItem.Name & " [" & reportItem.Type.ToString() & "]"
        lstFolders.Items.Add(strDisplay)
    Next
End Sub

I've shown two code pieces:

  1. The first snippet is the code to be written in the constructor. In this method, we create a new instance of the reporting service proxy class and then set the credentials for connecting to the web-service. The credentials is required for connecting to the web-service and executing various methods. Thus, the account that you use for this must have permissions to access the web-service. In the example above, I've used the default credentials which indicate the account current logged in.
  2. The second snippet is the code for the button. In this code, we first create an array of CatalogItem objects. The CatalogItem class represents an item in the report server database. We also create a iterator object of type CatalogItem to iterate through the array. The crux of the code is the call to the ListChildren method of the web-service which gets a list of children of a specified folder. The ListChildren method returns only child items that the user has permission to view. The items that are returned may not represent a complete list of child items of the specified parent item. Once we get the items, we then iterate through the items and then add them to the listbox. When adding to the listbox, we add the name of the item and its type which is referenced using the Type.ToString() construct.

Let us now run this program. When you run the program, you get the window designed above and when you click the button, you get the list of all items defined in the report server. Note that the display may differ based on what you have created in your report server. The following figure shows the output from my machine.

That's pretty cool eh! You can see all the items that are present in the report server and an explanation about the type of the resource. You can also print other properties of the resource if you need. But wait, do you feel that something is wrong in the display?? To check this out, see the following figure:

The above figure shows the contents of my root folder and we see that there are two items My Experiments and TimeSheet. Under each of these folders, I have other items (and possibly other folders too). But, in our display from the application, we saw all items listed in a single order with no apparent formatting done for the folder structure. For example, the New Folder folder in the figure above is actually under the My Experiments folder and Test Data Source is inside that folder. Our display however did not take care of this relationship, but it is easy to build such a structure and I leave it as an exercise for the reader to try it out.

Well, that brings us to the end of this quick introduction to the reporting services web-service. There are many other variations of this sample that you can build and in future articles we will explore some esoteric usage of the web-service. For now, in this article we had a quick look at the power that you have when you use the reporting service web service. Have fun!!

Home