Crystal Reports Web Services

book list add book to my bookshelf create a bookmark purchase this book online

mastering crystal reports 9
Chapter 22 - XML Web Services
Mastering Crystal Reports 9
by Cate McCoy and Gord Maric
Sybex 2003

Crystal reports can be built as web services and accessed by any application using the Windows Form Viewer in a .NET Windows application or the Web Form Viewer in an ASP.NET application. By exposing your Crystal report, you allow your customers to use your reports in their applications. Your customers do not need to have access to your database or copy the reports to their systems; all you have to provide is a Crystal report as a web service, and they can use the report from your web server in their applications.

Creating a Crystal Report Web Service

Building a Crystal report web service is a simple process. As we demonstrated in building the AddMe web service, the complexity of SOAP, WSDL, and XML have been hidden by Visual Studio.

  1. Start Visual Studio .NET, and create a new Visual Basic ASP.NET web service project. Call it CrWebService.

  2. In the Solution Explorer, right-click the Service1.asmx file and delete it. We will replace the Service1.asmx file with a Crystal report web service.

  3. Copy the Crystal report that you want to publish to your web server directory. We are doing this so the web server will have access to the report file. This is an easy way to make sure we have access to the report. The web server may not have access to the report file if it is in a different directory, alternatively you can change the permission in your web server.

  4. Back in Visual Studio, in the Solution Explorer, right-click the project name, choose Add Existing Item, and add the report that you copied to the web server. You will have to change the file type to display the Crystal report. We are going to add our report called resorts.rpt.

  5. Right-click the report you added and select Publish As Web Service, as shown in Figure 22.9. That’s all there is to making a Crystal report web service. Visual Studio will create a source code file that will publish the web service. The name of the source file will be the name of the report with Service.asmx appended to it. In our case, the report was called resorts.rpt. Visual Studio created resortsService.asmx. If you click the Show All icon in the Solution Explorer, you will see the files that are generated.

    click to expand
    Figure 22.9. Publishing a Crystal report as a web service

  6. Right-click the source file that was generated, resortsService.asmx in our case, and choose Set As Start Page. This will allow you to test the web service without having to build a client to make sure the report has been published as a web service.

  7. Save and build your application.

  8. Start the web service application by pressing F5. Visual Studio will display your web service page with a list of the methods that are exposed by the web service, as shown in Figure 22.10. You can click the various web methods to view the SOAP request and response.

    Warning 

    You will not be able to invoke these methods directly. The Crystal web service requires a Crystal Report Viewer to call these methods.

    click to expand
    Figure 22.10. Crystal web service methods available to the Crystal Viewer

Using the Web Service

The Crystal web service report can be accessed by either the Windows Form Viewer in a .NET Windows application or a Web Form Viewer in an ASP.NET application. Either way, the steps to view the Crystal reports are the same.

Start Visual Studio .NET and create a Windows application or an ASP.NET web application. From the toolbox, drag the Crystal Report Viewer and a command button to the form.

Note 

The Crystal Report Viewer is called a Windows Form Viewer in a Windows application and the Web Form Viewer in an ASP application.

To display the web service report, all we need to do is bind the Report Viewer to the web service. There are two ways to bind the viewer to the report:

Directly To bind the Report Viewer to the report service, simply set the ReportSource property of the viewer to the report web service:

Private Sub Button1_Click(ByVal sender As System.Object,    ByVal e As System.EventArgs) Handles Button1.Click CrystalReportViewer1.ReportSource = "http://localhost/crwebservice/resortsservice.asmx"     End Sub

Tip 

In the URL above, localhost represents the name of your web server and crwebservice is the virtual directory where you build you web service application. These may be different in your environment.

Web Reference Alternatively, you can create a web reference, as we did in the AddMe web service client. Visual Studio will build a proxy class to call the web service. To add a web reference, right-click the project in the Solution Explorer and choose Add Web Reference; then add the URL of the web service in the Address line. In our case, it is http://localhost/crwebservice/resortsservice.asmx CH01Service.asmx.

Private Sub Button1_Click(ByVal sender As      System.Object, ByVal e As System.EventArgs) Handles     Button1.Click         Dim wsResorts As New localhost.resortsService()         CrystalReportViewer1.ReportSource = wsResorts     End Sub

In either case, when you click Button1, the Crystal Report Viewer will call the web service and display the reports. All the SOAP, WSDL, and XML documents are automatically taken care of. Figure 22.11 displays the report from the web service.

Note 

If you are publishing your reports as a web service, you may need to secure the reports so that only authorized users can access the report. Microsoft’s Internet Information Server (IIS) web server provides a number of techniques for securing access and encrypting communication via Secure Sockets Layer (SSL or https). Refer to IIS documentation or your web administrator for more information.

click to expand
Figure 22.11. Displaying the web service report

Changing Selection Criteria

Once a report is bound to a report viewer, whether it is bound directly as we did in the previous chapters or though a web service, you can manage the report by working with the methods in the Crystal Report Viewer. The following example sets the selection criteria of the report so that we receive only “US” countries from the web service:

   CrystalReportViewer1.SelectionFormula =    "{Resorts.Country} = 'US'"    CrystalReportViewer1.ReportSource =     "http://localhost/crwebservice/resortsservice.asmx"

Use of content on this site is expressly subject to the restrictions set forth in the Membership Agreement
 
Conello © 2000-2003     Feedback


Mastering Crystal Reports 9
Mastering Crystal Reports 9
ISBN: 0782141730
EAN: 2147483647
Year: 2005
Pages: 217

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net