Export (0) Print
Expand All

Adding a Helper Method to Access Crystal Reports Web Services

Visual Studio 2005

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

Note   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.

To create a Helper Method to Access Crystal Reports Web Services

  1. Open the Web or Windows form.
  2. From the View menu, click Code.
  3. Add three private class level variables.
    [Visual Basic]
    Private myServerFileReport As ServerFileReport
    Private myReportManagerRequest As ReportManagerRequest
    Private myServerFileReportManagerProxy As ServerFileReportManagerProxy
    [C#]
    private ServerFileReport serverFileReport;
    private ReportManagerRequest reportManagerRequest;
    private ServerFileReportManagerProxy serverFileReportManagerProxy;
  4. Create a new private helper method that returns an ArrayList.
    [Visual Basic]
    Protected Function getReports() As ArrayList
    End Function
    [C#]
    protected ArrayList getReports()
    {
    }
  5. Inside of the helper method, instantiate a new ServerFileReport instance.
    [Visual Basic]
    myServerFileReport = New ServerFileReport()
    [C#]
    serverFileReport = new ServerFileReport();
  6. Set the ReportPath property of the ServerFileReport instance to a null string.
    [Visual Basic]
    myServerFileReport.ReportPath = ""
    [C#]
    serverFileReport.ReportPath = "";
  7. Set the WebServiceURL property of the ServerFileReport to the viewer's virtual directory 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 2005.

    [Visual Basic]
    myServerFileReport.WebServiceUrl = "http://localhost:80/CrystalReportsWebServices2005/serverfilereportservice.asmx"
    [C#]
    serverFileReport.WebServiceUrl = "http://localhost:80/CrystalReportsWebServices2005/serverfilereportservice.asmx";
    Note   This tutorial assumes that your report server is your local machine, thus you use http://localhost:80/ as the Web Service URL. In order to use a different machine as your report server, replace http://localhost:80/ with the address and port number of your report server.
  8. Instantiate a new ReportManagerRequest instance.
    [Visual Basic]
    myReportManagerRequest = New ReportManagerRequest()
    [C#]
    reportManagerRequest = new ReportManagerRequest();
  9. 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.
    [Visual Basic]
    myReportManagerRequest.ExtraData = myServerFileReport.GetExtraData()
    [C#]
    reportManagerRequest.ExtraData = serverFileReport.GetExtraData();
  10. 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.
    [Visual Basic]
    myReportManagerRequest.ParentUri = myServerFileReport.ToUri()
    [C#]
    reportManagerRequest.ParentUri = serverFileReport.ToUri();
  11. Instantiate a new ServerFileReportManagerProxy instance.
    [Visual Basic]
    myServerFileReportManagerProxy = New ServerFileReportManagerProxy()
    [C#]
    serverFileReportManagerProxy = new ServerFileReportManagerProxy();
  12. 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 2005.

    [Visual Basic]
    myServerFileReportManagerProxy.Url = "http://localhost:80/CrystalReportsWebServices2005/serverfilereportmanager.asmx"
    [C#]
    serverFileReportManagerProxy.Url = "http://localhost:80/CrystalReportsWebServices2005/serverfilereportmanager.asmx";
  13. Create and instantiate a new ReportManagerResponse object.
    [Visual Basic]
    Dim myReportManagerResponse As ReportManagerResponse = new ReportManagerResponse()
    [C#]
    ReportManagerResponse reportManagerResponse = new ReportManagerResponse();
  14. 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.
    [Visual Basic]
    myReportManagerResponse = myServerFileReportManagerProxy.ListChildObjects(myReportManagerRequest)
    [C#]
    reportManagerResponse = serverFileReportManagerProxy.ListChildObjects(reportManagerRequest);
  15. Create and instantiate a new ArrayList.
    [Visual Basic]
    Dim myRemoteReports As ArrayList = New ArrayList()
    [C#]
    ArrayList remoteReports = new ArrayList();
  16. Next, create a For Each loop that loops through each element of reportManagerResponse.
    [Visual Basic]
    For Each myReportUriString As String In myReportManagerResponse.ReportUris
    
    Next
    [C#]
    foreach (string reportUriString in reportManagerResponse.ReportUris)
    {
    }
  17. Inside of the For Each loop, create a new instance of serverFileReport.
    [Visual Basic]
    myServerFileReport = New ServerFileReport()
    [C#]
    serverFileReport = new ServerFileReport();
  18. Set the serverFileReport Uri to the value of reportUriString.
    [Visual Basic]
    myServerFileReport = ServerFileReport.FromUri(myReportUriString)
    [C#]
    serverFileReport = ServerFileReport.FromUri(reportUriString);
  19. 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   The ListChildObjects method will return both directories and reports. In this tutorial you will filter the list of objects for report files.
    [Visual Basic]
    If (myServerFileReport.ObjectType = EnumServerFileType.REPORT) Then
    End If
    [C#]
    if (serverFileReport.ObjectType == EnumServerFileType.REPORT)
    {
    }
  20. Inside of the If conditional block, add the reportUriString instance to the remoteReport ArrayList.
    [Visual Basic]
    myRemoteReports.Add(myReportUriString)
    [C#]
    remoteReports.Add(reportUriString);
  21. Finally, outside of the For Each loop, return the remoteReports ArrayList.
    [Visual Basic]
    Return myRemoteReports
    [C#]
    return remoteReports;
  22. 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.

See Also

Tutorial: Populating a DropDown List of Reports from a Web Service | Tutorials and Sample Code

Show:
© 2014 Microsoft