Professional.Crystal.Reports.for.Visual.Studio..NET [Electronic resources]

David McAmis

نسخه متنی -صفحه : 115/ 48
نمايش فراداده

Creating XML Report Web Services

The process of creating XML Report Web Services is relatively simple and features a number of shortcuts to help cut down on development time. Unlike the process you would use to create web services from scratch, there is no custom coding required to publish an existing Crystal Report file to a XML Report Web Service.

Creating Basic Report Web Services

To get started with this chapter, we need to create a new Windows Application using Visual Basic. NET. If you want to follow along with the sample code that is available for this chapter, you can open the solutions file (Chapter5.sln) and you will find a number of projects within that correspond to the sections in this chapter. To create your own project as we go along, create a new project from within Visual Studio by selecting File | New | Project and from Visual Basic Applications, select ASP.NET Web Service and specify a name (in the sample code, we have called this project WebService1) and location on your web server for your project files.

In this example, we are going to create a Report Web Service from the inventory report (inventory_report.rpt), one of the reports included with the code samples (in our installation we have downloaded this to C:\CrystalReports\Chapter05\inventory_report.rpt; you may wish to alter this depending on your own installation).

To add this report to your web service, select Add | Existing Item and browse to where you have unzipped your code files and select the inventory_report.rpt

Once you have added this report to your project, it should appear in the resources and you can edit the report design and content as required. For now, we'll leave the content as-is until we start looking at some of the more advanced features.

With the report added, simply right-click on the report itself in the Project Explorer and from the right-click menu, select Publish as web service as shown below:

This will in turn generate an ASMX file that will also appear in the Project Explorer. There is also a source file generated for your Report Web Service, but by default it is hidden. To show the source file, select the option for Show All Files in the solution explorer and you should see all three files, as shown in the screenshot overleaf.

Remember to compile inventory_reportService.asmx before you attempt to run it. You can do this by selecting Build from your menu or hitting F5 on your keyboard.

Note

Be sure that the report you are publishing as a Web Service does not have a comment as the first line of the record selection formula, as the selection formula will be ignored.

To edit your record selection formula, open the report in the Report Designer, right-click on the report and select Report | Edit Selection Formula | Record and remove or move the first line.

This issue has been tracked and will hopefully be fixed in future releases. For Report Web Services created using Visual Basic, the Web Service file will have the extension .vb tacked on the end. You can alter this source file to add additional functionality, launch other web services, and perform other functions.

With your report published as a Web Service, you should be able to right-click directly on the corresponding .asmx file and select View in Browser from the right-click menu to display a list of valid operations for your web service, as shown below:

The URL for your web service in this example is:

http://localhost/WebService1/inventory_reportService.asmx

but if our report name had a space in it for example "Inventory Report" the space would be encoded and the URL would look like this:

http://localhost/WebService1/Inventory%20ReportService.asmx

Note

A good practice for Report Web Services is to ensure that the name of your report does not have any spaces in it - in this instance renaming the report to remove the space or replace it with an underscore would probably be easier than remembering to put in the spaces or correct encoding when calling your Report Web Service. To toggle the formal definition, select the link for Service Description to display the XML document shown here:

When we look at consuming XML Report Web Services a little later in this chapter, we will look at some of these methods and their use.

Creating Report Web Services with Multiple Reports

If you have multiple reports that you would like to publish as XML Report Web Services, you can place all of these report files into a web service project by following the same procedure as a single report file.

Simply add each report from the Project | Add Existing Item menu and then right-click on each and select Publish as Web Service. To view the Report Web Service for a specific report, just reference the name of the correct .asmx file associated with that report - again, remember to compile them before trying to view them.

For example if you were to add a report named sales_graph.rpt to the project we have been working with and publish it as a web service, the URL reference would be:

http://localhost/WebService1/sales_graphService.asmx

This is an easy way to keep related reports and services together and can serve as the basis for a naming and hierarchy structure for your Report Web Services.

Utilizing the Generic Report Web Service

In addition to creating individual Report Web Services for each report you wish to publish as a Web Service, Crystal Reports.NET also includes a generic Web Report Service, which supports what are known as Server Reports.

Server Reports are Crystal Report files that can be accessed through a generic Report Web Service (ServerFileReportService.asmx) and are available for use from the Server Explorer within the Visual Studio .NET IDE, as shown below:

When working with the generic Report Web Service, there is no need to publish each report individually, but there are a couple of properties that you will need to set when viewing your reports using this method.

To view a report through the generic Report Web Service, you will need to specify the Web Service URL that points to the generic Report Web Service, which in turn, accesses the report specified in the ReportPath property. With the ReportPath, you don't actually need to put the full path of the report because this property references a root directory that is specified in an XML filed called crystalserverfile.config, shown below, which can be found in C:\Program Files\Microsoft visual Studio.NET\Crystal Reports\Config on the server.

Note

This file also controls what reports you will see in the IDE under the Server Files node of the Crystal Services branch of your server. When you first install Crystal Reports.NET, the RootDirectory tag within the file refers to the sample reports that ship with the product, so when you browse the Server Files node of the Server Explorer within the Visual Studio IDE, you are actually starting at the C:\Program Files\Microsoft Visual Studio.NET\Crystal Reports\Samples\Reports directory. The URL to access this Web Service is:

http://localhost/crystalreportwebformviewer/ServerFileReportService.asmx

If you don't see any reports underneath the Server Files node, check to see if you have the Web Services component of Crystal Reports.NET installed. If you took all of the defaults on the Visual Studio .NET installation, you wouldn't have this option installed, as shown below.

You will need to re-run the setup wizard to add this component before you can use the generic Report Web Service. If you believe you have installed the component and still don't see the crystalserverfile.config, you can create another using the XML code below, replacing the location in the <RootDirectory> tag with the location of your reports.

<?xml version="1.0" encoding="utf-8"?>
<ServerFileConfig
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<RootDirectory>C:\Program Files\Microsoft Visual Studio
.NET\Crystal Reports\Samples\Reports</RootDirectory>
</ServerFileConfig>

You may also experience problems if the generic report web service does not have write permissions to the folder specified in the crystalserverfile.config document. One workaround is to grant the ASPNET account read and write access to the C:\program files\Microsoft Visual Studio.NET\Crystal Reports\ directory or simply edit the crystalserverfile.config file and point it to a directory where the correct permissions have been set.

Again, this is another issue that has been tracked by Crystal Decisions and should be fixed in future releases.