Nov

12

Web Service To Render Reporting Service Report To PDF

Written by Duncan White

If you would ever need to make a web service call SQL Reporting Services, and store the report into a pdf file to be retreived at a later time please follow the below example.

Add web references ReportingServices2005 and ReportingExecution2005 to your web service project. Then add the following code to your code behind.

  Private Sub GenerateReport(ParamList as hashtable)
        rs.Credentials = System.Net.CredentialCache.DefaultCredentials
        rsExec.Credentials = System.Net.CredentialCache.DefaultCredentials

        Dim historyID As String = Nothing
        Dim deviceInfo As String = Nothing
        Dim format As String = "PDF"
        Dim results As Byte()
        Dim encoding As String = String.Empty
        Dim mimeType As String = String.Empty
        Dim extension As String = String.Empty
        Dim warnings As ReportExecution.Warning() = Nothing
        Dim streamIDs As String() = Nothing
        Dim filename As String = "C:\MyReport.pdf"        ' Change to where you want to save
        Dim _reportName As String = "/Sales/MyReport"  ' Change to be your report
        Dim _historyID As String = Nothing
        Dim _forRendering As Boolean = False
        Dim _values As ReportingService.ParameterValue() = Nothing
        Dim _credentials As ReportingService.DataSourceCredentials() = Nothing
        Dim _parameters As ReportingService.ReportParameter() = Nothing

        _parameters = rs.GetReportParameters(_reportName, _historyID, _forRendering, _values, _credentials)

        Dim ei As ReportExecution.ExecutionInfo = rsExec.LoadReport(_reportName, historyID)
        Dim parameters(_parameters.Length - 1) As ReportExecution.ParameterValue

        for param as integer = 0 to _parameters.count - 1
            parameters(param) = New ReportExecution.ParameterValue
            parameters(param).Label = _parameters(param).name
            parameters(param).Name = _parameters(param).name
            parameters(param).Value = ParamList(param)
        next

        rsExec.SetExecutionParameters(parameters, "en-us")
        results = rsExec.Render(format, deviceInfo, extension, mimeType, encoding, warnings, streamIDs)

        Dim stream As New System.IO.FileStream(filename, IO.FileMode.OpenOrCreate)

        stream.Write(results, 0, results.Length)
        stream.Close()
    End Sub

This will work great if you are still using your Reporting Service as it came out of the box. Although if you have read my blog Reporting Services 2005 Forms Authentication (Click Here To Read Article) which covered making Reporting Services Forms Authenticated, you will need to make a modification to the above code.

 rs.Credentials = System.Net.CredentialCache.DefaultCredentials
 rsExec.Credentials = System.Net.CredentialCache.DefaultCredentials

Needs to be changed to the following

 rs.Credentials = New System.Net.NetworkCredential("uesrname", "password")
 rsExec.Credentials = New System.Net.NetworkCredential("uesrname", "password")

Leave a Reply