Adding a Helper Method to Access Crystal Reports Web Services

This procedure requires access to a machine that has been configured to use Crystal Reports web services. Before proceeding, please ensure that your machine has been configured correctly. See Configuring Server Files in Crystal Services for more information.

In this section, you create a helper method that will access a server through Crystal Reports web services and generate a list of reports.

  1. Open the Web or Windows form.

  2. From the View menu, click Code.

  3. Add three private class level variables.

    Private myServerFileReport As ServerFileReport
    Private myReportManagerRequest As ReportManagerRequest
    Private myServerFileReportManagerProxy As
    ServerFileReportManagerProxy
    
    private ServerFileReport serverFileReport;
    private ReportManagerRequest reportManagerRequest;
    private ServerFileReportManagerProxy serverFileReportManagerProxy;
    
  4. Create a new private helper method that returns an ArrayList.

``` vb
Protected Function getReports() As ArrayList
End Function
```

``` csharp
protected ArrayList getReports()
{
}
```
  1. Inside of the helper method, instantiate a new ServerFileReport instance.
``` vb
myServerFileReport = New ServerFileReport()
```

``` csharp
serverFileReport = new ServerFileReport();
```
  1. Set the ReportPath property of the ServerFileReport instance to a null string.
``` vb
myServerFileReport.ReportPath = ""
```

``` csharp
serverFileReport.ReportPath = "";
```
  1. Set the WebServiceURL property of the ServerFileReport to the viewer's virtual directory for your installed version of Crystal Reports, see Viewers' Virtual Directory.
> [!NOTE]
> <P>This tutorial assumes that your report server is your local machine, thus you use https://localhost:80/ as the Web Service URL. In order to use a different machine as your report server, replace https://localhost:80/ with the address and port number of your report server.</P>


In this sample code, the viewers' virtual directory is configured for Crystal Reports for Visual Studio 2008.

``` vb
myServerFileReport.WebServiceUrl =
"https://localhost:80/CrystalReportsWebServices/serverfilereportservice.asmx"
```

``` csharp
serverFileReport.WebServiceUrl =
"https://localhost:80/CrystalReportsWebServices/serverfilereportservice.asmx";
```
  1. Instantiate a new ReportManagerRequest instance.
``` vb
myReportManagerRequest = New ReportManagerRequest()
```

``` csharp
reportManagerRequest = new ReportManagerRequest();
```
  1. Assign the result of the GetExtraData method of the serverFileReport instance to the ExtraData property of the ReportManagerRequest instance. This sets the ExtraData property to the Web Service URL.
``` vb
myReportManagerRequest.ExtraData = myServerFileReport.GetExtraData()
```

``` csharp
reportManagerRequest.ExtraData = serverFileReport.GetExtraData();
```
  1. Assign the result of the ToUri method of the serverFileReport instance to the ParentUri property of the ReportManagerRequest instance. This identifies the directory on the server machine that contains the reports to be listed.
``` vb
myReportManagerRequest.ParentUri = myServerFileReport.ToUri()
```

``` csharp
reportManagerRequest.ParentUri = serverFileReport.ToUri();
```
  1. Instantiate a new ServerFileReportManagerProxy instance.
``` vb
myServerFileReportManagerProxy = New ServerFileReportManagerProxy()
```

``` csharp
serverFileReportManagerProxy = new ServerFileReportManagerProxy();
```
  1. Set the Url property of the ServerFileReportManagerProxy to the Server File Report Manager for your installed version of Crystal Reports, see Viewers' Virtual Directory.
In this sample code, the viewers' virtual directory is configured for Crystal Reports for Visual Studio 2008.

``` vb
myServerFileReportManagerProxy.Url = "https://localhost:80/CrystalReportsWebServices/serverfilereportmanager.asmx"
```

``` csharp
serverFileReportManagerProxy.Url = "https://localhost:80/CrystalReportsWebServices/serverfilereportmanager.asmx";
```
  1. Create and instantiate a new ReportManagerResponse object.
``` vb
Dim myReportManagerResponse As ReportManagerResponse = new ReportManagerResponse()
```

``` csharp
ReportManagerResponse reportManagerResponse = new ReportManagerResponse();
```
  1. Call the ListChildObjects() method of the serverFileReportManagerProxy instance and assign the result to the ReportManagerResponse instance. The ListChildObjects method returns a list of files located on the report server.
``` vb
myReportManagerResponse = myServerFileReportManagerProxy.ListChildObjects(myReportManagerRequest)
```

``` csharp
reportManagerResponse = serverFileReportManagerProxy.ListChildObjects(reportManagerRequest);
```
  1. Create and instantiate a new ArrayList.
``` vb
Dim myRemoteReports As ArrayList = New ArrayList()
```

``` csharp
ArrayList remoteReports = new ArrayList();
```
  1. Next, create a For Each loop that loops through each element of reportManagerResponse.

    For Each myReportUriString As String In myReportManagerResponse.ReportUris
    
    Next
    
    foreach (string reportUriString in reportManagerResponse.ReportUris)
    {
    }
    
  2. Inside of the For Each loop, create a new instance of serverFileReport.

``` vb
myServerFileReport = New ServerFileReport()
```

``` csharp
serverFileReport = new ServerFileReport();
```
  1. Set the serverFileReport Uri to the value of reportUriString.
``` vb
myServerFileReport = ServerFileReport.FromUri(myReportUriString)
```

``` csharp
serverFileReport = ServerFileReport.FromUri(reportUriString);
```
  1. Still inside of the For Each loop, create an If conditional block that verifies that the ObjectType property of the ServerFileReport is of type REPORT.
> [!NOTE]
> <P>The ListChildObjects method will return both directories and reports. In this tutorial you will filter the list of objects for report files.</P>


``` vb
If (myServerFileReport.ObjectType = EnumServerFileType.REPORT) Then
End If
```

``` csharp
if (serverFileReport.ObjectType == EnumServerFileType.REPORT)
{
}
```
  1. Inside of the If conditional block, add the reportUriString instance to the remoteReport ArrayList.
``` vb
myRemoteReports.Add(myReportUriString)
```

``` csharp
remoteReports.Add(reportUriString);
```
  1. Finally, outside of the For Each loop, return the remoteReports ArrayList.
``` vb
Return myRemoteReports
```

``` csharp
return remoteReports;
```
  1. From the File menu, click Save All.

In the next step, you write code to automatically populate the DropDownList or ComboBox control with a list of reports.

For a Web Site, continue to Populating the DropDownList Control in a Web Site.

For a Windows Project, continue to Populating the ComboBox Control in a Windows Project.