SQL Server > SQL Server Forums > SQL Server Reporting Services > call ssrs 2008 from c# and save to PDF?
Ask a questionAsk a question
 

Questioncall ssrs 2008 from c# and save to PDF?

  • Monday, November 23, 2009 7:49 PMjaviguillen Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi, i have found examples on how to call SSRS 2005 from C# but not 2008.  I see the "reportservice2005.asmx" endpoint is still available on 2008 but the methods seem to be different, and I cannot apply the same examples i find online for 2005.  (for example, the interface does not have a "Url" property)

    how can I call SSRS 2008 from C# (not ASP.NET, just a console app) and generate a PDF copy of the report?
    Javier Guillen

All Replies

  • Thursday, November 26, 2009 2:06 AMJin ChenMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code

    Hi Javier,

    The endpoint for SQL Server Reporting Services 2008 is same to the endpoint for SQL Server Reporting Services 2005. That means the sample code for SQL Server Reporting Services is also good for SQL Server Reporting Services 2008.

    Below is the code used to generate a PDF copy of the report:

    Dim reportPath As String = "vReportPath"
    Dim format As String = "vFormat"
    Dim fullPathOfOutputFile = "vFullPathOfOutputFile"
    
    Public Sub Main()
        ' Authenticate to the Web service using Windows credentials 
        rs.Credentials = System.Net.CredentialCache.DefaultCredentials 
        Dim report As Byte() = Nothing 
        Dim deviceinfo As String = Nothing 
        Dim parameters As ParameterValue() = Nothing 
        Dim historyID As String = Nothing 
        Dim credentials As DataSourceCredentials() = Nothing 
        Dim showHideToggle As String = Nothing 
        Dim extension As [String] = String.Empty 
        Dim encoding As [String] = String.Empty 
        Dim mimeType As [String] = String.Empty 
        Dim warnings As Warning() = Nothing 
        Dim reportHistoryParameters As ParameterValue() = Nothing 
        Dim streamIDs As String() = Nothing 
        Dim execInfo As New ExecutionInfo() 
        Dim execHeader As New ExecutionHeader() 
        rs.ExecutionHeaderValue = execHeader 
        execInfo = rs.LoadReport(reportpath, historyID) 
        Try 
            report = rs.Render("PDF", deviceinfo, extension, mimeType, encoding, warnings, streamIDs)   
        
            Dim fs As New FileStream(fullPathOfOutputFile, FileMode.Create) 
            fs.Write(report, 0, report.Length)         
            fs.Close() 
        Catch ex As Exception 
            Console.WriteLine(ex.Message)
        End Try
    End Sub
    



    Please feel free to ask, if you have any more questions.

    Thanks,
    Jin Chen


    Jin Chen - MSFT
  • Wednesday, December 02, 2009 1:04 AMTau LiangMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Javier,
    The MSDN document below has a C# sample console application rendering to MHTML. Change "format" + corresponding deviceinfo and you'll get something for PDF.
    http://msdn.microsoft.com/en-us/library/microsoft.wssux.reportingserviceswebservice.rsexecutionservice2005.reportexecutionservice.render.aspx

    Please note that the "Render()" method is part of the rsexecutionservice2005SOAP API.
    Hope it helps.


    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Tuesday, December 08, 2009 5:33 PMjaviguillen Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Sorry it took me a bit of time to respond. I am still having problems getting this to work. Tried to follow the approach you outlined above, when when I reference the service as http://myserver/reportserver/reportservice2005.asmx

    I do not have access to a "ReportExecutionService" class on it.

    closest thing I see is "ReportingService2005SoapClient", but there is no Render or LoadReports method in there!

    am I missing something?  how can I access the right class?


    Javier Guillen
  • Wednesday, December 09, 2009 3:06 AMJin ChenMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Javier,

    I assume you have used a WCF service client (proxy) class that is generated for ReportServices2005 by Visual Studio when a service reference is added. 

     

    Please read the following article for more information:

    Building Applications Using the Web Service and the .NET Framework: http://msdn.microsoft.com/en-us/library/ms154699.aspx

    Please follow these steps to add a web reference, and then we are able to the access the LoadReports or other reporting services web services methods:
    1.Right-click on the project
    2.Click "Add web reference"(not "Add service reference")
    3.In the URL textbox, type http://myserver/reportserver/reportservice2005.asmx  or http://myserver/reportserver/reportexecution2005.asmx
    http://myserver/reportserver/reportservice2005.asmx is the endpoint for report services.
    http://myserver/reportserver/reportexecution2005.asmx is the endpoint for the report execution services.
    4.Then click "Go", the web services will be found.
    5.Type a name for the reference, and click "Add reference" to apply.
    6.Now, we can call the web services using the following code:
                <referenceName>.ReportExecutionService rsExec = new <NameSpace>.<referenceName>.ReportExecutionService();
                rsExec.LoadReport(...);

    For more information, please see:
    http://msdn.microsoft.com/en-us/library/cc282207.aspx

    Thanks,
    Jin Chen


    Jin Chen - MSFT
  • Wednesday, December 09, 2009 2:52 PMDoyle J. Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Jin , when we follow your link to:

    http://msdn.microsoft.com/en-us/library/reportservice2005.aspx


    we see that a class called ReportingService2005 is included::   HOWEVER

    when we establish a service reference as you describe above and use the object browser within visual studio
    we do not see this class . at least I do not .. I see a ReportingService2005SoapClient class...

    Please continue to help us on this issue... Doyle
    Doyle
  • Wednesday, December 09, 2009 3:07 PMjaviguillen Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Doyle,

    yes what Jin is trying to show us is that the example using the ReportingService2005 class is when the service is accessed as a 'web service' reference.   If you use a 'service reference', instead, which uses WCF instead of Web service approach, you get the ReportingService2005SoapClient class which implements the service a bit differently...

    I got it to work as a web service, but ideally i'll like to have the code for WCF access too, as my idea was to create a console app for this and I can't seem to add a web service reference to it (only service references/WCF)


    Javier Guillen
  • Wednesday, December 09, 2009 3:18 PMDoyle J. Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Ok Javieer thanks my friend ... what if we want to take this a step forward....say we want to use a console app to DEPLOY a report  ... how do we do that without the web service reference or get the web service reference on the fly ... perhaps by having the user input the url to the reporting services server ???
    Doyle
  • Thursday, December 10, 2009 1:25 AMJin ChenMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi Javier, Doyle,

    Based on your description, you are going to call the Reporting Services web services in a Console application.

    To invoke a Web Service in a Console applicatoin, we can generate a proxy class for the Web Service. After that, set the Web Service’s URL to the proxy class, and then call the methods in the Web Service normally.

     

    Here are the detailed instructions:

    1.       Generate a Proxy class for the Web Service

    Sample: C:\>wsdl /l:CS /protocol:SOAP http://localhost/ReportServer/ReportService2005.as

    mx?WSDL.

    2.       A proxy class file named ‘ReportService2005.cs’ will be generated in the current folder.

    3.       Copy the proxy class file to your project folder.

    4.       Include the proxy class file in your project.

    5.       Use the following code to call the Web Service:

    ReportService2005 rs = new ReportService2005();

    rs.URL = <The URL of the Reporting Services web services>;

                   //e.g. rs.URL = "http://myserver/reportserver/reportexecution2005.asmx";      
     

    If you have any more questions, please feel free to ask.

     

    Thanks,

    Jin Chen


    Jin Chen - MSFT