Login   Register 
Receive news via our XML/RSS feed
Home | Book | Contents | Preview | Articles | Tools | Newsletters | Premium | Ask | About

Home

Book

Contents

Preview

Articles

Tools

Newsletters

Premium

Ask

About

Most Popular Articles
  • Using Secure Sockets Layer (SSL) for SQL Server 2000 Reporting Services
    Creating an SSL Certificate to ensure your confidential reports remain confidential


  •  
    Boost Data Ltd.
     
     
    SQL Reporting Services - Answers to your questions
     
    Author Thread: Web Export to PDF with SOAP, Forcing Download of PDF: an example
    pinolian

    Web Export to PDF with SOAP, Forcing Download of PDF: an example
    Posted: Friday, November 12, 2004 10:12 PM (GST)

    I needed to have my solution do the following, primarily due to our third party fax software's requirements:

     

    In one submit button on an ASPX page, I needed to do the following:

     

    1. Generate a report programattically on the intranet, based on parameters submitted in by a web form.

    2. Export the report to PDF automatically.

    3. Name the PDF programattcally in such a way that the 3rd party Fax software could use it automatically.

    4. Have adobe launch automatially on the client computer with the PDF, so all the user had to do was print within 2 key strokes.

     

    This is what I did:

     

    I had the report working fine when I was debugging it. So the report itself was good to go. With this example its assumed you also have a report that is working fine.

     

    The submit button in question on the web form just did a redirect to an empty web form. I deleted all of the HTML in the page, leaving the page language stuff at the top.

     

    You have to make a web reference to your report server, in your web solution . Just right click on your web solution, click add web reference, and in the box, on the page that pops up, put in your report server as follows:

     

    http://yourservername/ReportServer/ReportService.asmx

     

    Give it a name, mine was named hbs2. put that name in the box on the lower right. Click ok.

     

    Add the following imports:

     

    Imports System
    Imports System.IO 'for writing the PDF
    Imports System.Web.Services.Protocols ' for soap exception

     

    Where ever you write the report to on the web server you have to grant WRITE permissions to the User Account that is running the aspnet_wp.exe process.

    So in my example, I have given the ASPNET account write permissions on the web server to the C:\Temp folder.

     

    In the page load I have the following code:

     

             Dim format As String = "PDF"

            'This is the name/location where the report will be generated on the web server

            'the best part is that you can name the actual file anything you want in this code

           'give write permissions to this folder:
            Dim fileName As String = "C:\Temp\Tester.PDF"

     

            'name of report on the report server that you want to use

            'its basically / project name / report name
            Dim reportPath As String = "/NDPReports/Report1"

            Dim historyID As String = Nothing
            Dim deviceInfo As String = Nothing
            Dim showHide As String = Nothing


            'results is the file stream that we will be using

            Dim results() As Byte
            Dim encoding As String
            Dim mimeType As String

      

           'hbs2 is the name of my web ref to the reportserver,

            Dim warnings() As hbs2.Warning = Nothing

            Dim reportHistoryParameters() As hbs2.ParameterValue = Nothing

            Dim streamIDs() As String = Nothing

     

            'The actual reporting service

            Dim rs As New hbs2.ReportingService


            rs.Credentials = System.Net.CredentialCache.DefaultCredentials

     

            'report parameters, passed in by querystring

            Dim Parameters(1) As hbs2.ParameterValue


            Parameters(0) = New hbs2.ParameterValue
            Parameters(0).Name = "Initials"
            Parameters(0).Value = Request.QueryString("Initials")


            Parameters(1) = New hbs2.ParameterValue
            Parameters(1).Name = "WorkDate"
            Parameters(1).Value = Request.QueryString("WorkDate")

         

           'this is where the report is generated

            results = rs.Render(reportPath, format, historyID, deviceInfo, Parameters, _
            Nothing, showHide, encoding, mimeType, _
            reportHistoryParameters, warnings, streamIDs)

     

            'Write the file to the webserver, using what ever name you want!

            Dim stream As FileStream = File.OpenWrite(fileName)
            stream.Write(results, 0, results.Length)
            stream.Close()

            DownloadFile(fileName, True)

            Response.ClearContent()
            Response.ClearHeaders()
            Response.ContentType = "application/pdf"


    ------This is the Download file function that forces the download------------

     

        Private Sub DownloadFile(ByVal fname As String, ByVal forceDownload As Boolean)
            Dim path As Path
            Dim fullpath = path.GetFullPath(fname)
            Dim name = path.GetFileName(fullpath)
            Dim ext = path.GetExtension(fullpath)
            Dim type As String = ""

            If Not IsDBNull(ext) Then
                ext = LCase(ext)
            End If

            Select Case ext
                Case ".pdf", ".html"
                    type = "Application/pdf"
                Case ".txt"
                    type = "text/plain"
                Case ".doc", ".rtf"
                    type = "Application/msword"
                Case ".csv", ".xls"
                    type = "Application/x-msexcel"
                Case Else
                    type = "text/plain"
            End Select

            If (forceDownload) Then
                Response.AppendHeader("content-disposition", "attachment; filename=" + name)
            End If
            If type <> "" Then
                Response.ContentType = type
            End If

            Response.WriteFile(fullpath)
            Response.End()

        End Sub

     

     

    Now here is the thing.. I needed the download file function because the Save Open dialogue box allowed me to name the file exactly as I wanted to. If you just force adobe to open with

     

    Response.BinaryWrite(results)
    Response.ContentType = "application/pdf"

     

    Adobe will open, but the name of the file will include the entire path on the webserver, as opposed to the pristine name we needed (something like @201 name @@202 1115553333@.pdf )

     

    Without the extra code, and the extra click the 3rd party fax stuff would have been broken, trying to send a fax using C:\Temp\@201 name @@202 1115553333 @.pdf instead

     

    I hope this code works out for you. It took me about a day to put it together. The book certainly helped me, and I want to give credit where credit is due. However, forcing the download and working with the parameters in code were areas I had to research myself (btw if you need a null value, just pass in an object set to nothing).

     

    Good Luck

     

    -Pinolian

     

     

    Comments:  

    Author Thread:
    sunnergren
    Web Export to PDF with SOAP, Forcing Download of PDF: an example
    Posted: Friday, December 17, 2004 1:12 PM (GST)

    Good morning,

     

    I tried your code but got an authority error when I attempted to run it. I have a pretty generic W2k3 server with a clean RS install. The report works fine stand-alone but I get the following when trying to call it from your program . . .

     

    Server Error in '/ReportAccess' Application.
    --------------------------------------------------------------------------------

    The request failed with HTTP status 401: Unauthorized.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Net.WebException: The request failed with HTTP status 401: Unauthorized.

    Source Error:


    Line 547:"", RequestNamespace:="http://schemas.microsoft.com/sqlserver/2003/12/reporting/reportingservices", ResponseNamespace:="http://schemas.microsoft.com/sqlserver/2003/12/reporting/reportingservices", Use:=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle:=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)>  _
    Line 548:        Public Function Render(ByVal Report As String, ByVal Format As String, ByVal HistoryID As String, ByVal DeviceInfo As String, ByVal Parameters() As ParameterValue, ByVal Credentials() As DataSourceCredentials, ByVal ShowHideToggle As String, ByRef Encoding As String, ByRef MimeType As String, ByRef ParametersUsed() As ParameterValue, ByRef Warnings() As Warning, ByRef StreamIds() As String) As <System.Xml.Serialization.XmlElementAttribute("Result", DataType:="base64Binary")> Byte()
    Line 549:            Dim results() As Object = Me.Invoke("Render", New Object() {Report, Format, HistoryID, DeviceInfo, Parameters, Credentials, ShowHideToggle})
    Line 550:            Encoding = CType(results(1),String)
    Line 551:            MimeType = CType(results(2),String)
     

    Source File: C:\Sites\LibertyDEV\ReportAccess\Web References\AppReportService\Reference.vb    Line: 549

    Stack Trace:


    [WebException: The request failed with HTTP status 401: Unauthorized.]
       System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall) +1295
       System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters) +218
       ReportAccess.AppReportService.ReportingService.Render(String Report, String Format, String HistoryID, String DeviceInfo, ParameterValue[] Parameters, DataSourceCredentials[] Credentials, String ShowHideToggle, String& Encoding, String& MimeType, ParameterValue[]& ParametersUsed, Warning[]& Warnings, String[]& StreamIds) in C:\Sites\LibertyDEV\ReportAccess\Web References\AppReportService\Reference.vb:549
       ReportAccess.WebForm1.Page_Load(Object sender, EventArgs e) in C:\Sites\LibertyDEV\ReportAccess\ReportAccessMenu.aspx.vb:104
       System.Web.UI.Control.OnLoad(EventArgs e) +67
       System.Web.UI.Control.LoadRecursive() +35
       System.Web.UI.Page.ProcessRequestMain() +731

     


    --------------------------------------------------------------------------------
    Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET Version:1.1.4322.573

     

     

     

     

    Any help would be greatly appreciated.

     

    -Sunnergren

    jpvalentin
    Web Export to PDF with SOAP, Forcing Download of PDF: an example
    Posted: Friday, December 17, 2004 1:22 PM (GST)

    In the code, you can see the line :

    rs.Credentials = System.Net.CredentialCache.DefaultCredentials

     

    This meens that your NT session account is used to connect to the web service. Your account must have NTFS rights (Read&Execute, List Folder and Read) on  the folder and sub-folder :

     

    C:\Program Files\Microsoft SQL Server\MSSQL\Reporting Services\ReportServer

     

    The better thing to do, is to create a NT group with rights on the folder and add users into.

     

    JP VALENTIN

    sunnergren
    Web Export to PDF with SOAP, Forcing Download of PDF: an example
    Posted: Friday, December 17, 2004 1:44 PM (GST)

    Thank you, but I did read your response to the other gentleman who had a similar issue and I have temporarily granted full access to "everyone" and I still get this error.

     

    -Sunnergren

    jpvalentin
    Web Export to PDF with SOAP, Forcing Download of PDF: an example
    Posted: Friday, December 17, 2004 2:13 PM (GST)

    Granted full access to "everyone" is not the solution, if your IIS virtual directory as got anonymous access, so give the rights describe above to the IUSR_ account.

    (This is not recommended).

     

    The better way is to create a NT group with accounts.

     

    JP VALENTIN

    sunnergren
    Web Export to PDF with SOAP, Forcing Download of PDF: an example
    Posted: Friday, December 17, 2004 2:57 PM (GST)

    Okay, I created a new domain user group called ReportingServicesUsers, added myself, and granted the rights to the subdir and all items in the subdir and its bin subdir.

     

    Same error.

     

    This is without anonymous access enabled. When I enabled anonymous access, I get the following, different error:

     

    Server Error in '/ReportAccess' Application.
    --------------------------------------------------------------------------------

    The permissions granted to user 'TIBURON\IUSR_MARLIN' are insufficient for performing this operation. --> The permissions granted to user 'TIBURON\IUSR_MARLIN' are insufficient for performing this operation. --> The permissions granted to user 'TIBURON\IUSR_MARLIN' are insufficient for performing this operation.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Web.Services.Protocols.SoapException: The permissions granted to user 'TIBURON\IUSR_MARLIN' are insufficient for performing this operation. --> The permissions granted to user 'TIBURON\IUSR_MARLIN' are insufficient for performing this operation. --> The permissions granted to user 'TIBURON\IUSR_MARLIN' are insufficient for performing this operation.

    Source Error:


    Line 547:"", RequestNamespace:="http://schemas.microsoft.com/sqlserver/2003/12/reporting/reportingservices", ResponseNamespace:="http://schemas.microsoft.com/sqlserver/2003/12/reporting/reportingservices", Use:=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle:=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)>  _
    Line 548:        Public Function Render(ByVal Report As String, ByVal Format As String, ByVal HistoryID As String, ByVal DeviceInfo As String, ByVal Parameters() As ParameterValue, ByVal Credentials() As DataSourceCredentials, ByVal ShowHideToggle As String, ByRef Encoding As String, ByRef MimeType As String, ByRef ParametersUsed() As ParameterValue, ByRef Warnings() As Warning, ByRef StreamIds() As String) As <System.Xml.Serialization.XmlElementAttribute("Result", DataType:="base64Binary")> Byte()
    Line 549:            Dim results() As Object = Me.Invoke("Render", New Object() {Report, Format, HistoryID, DeviceInfo, Parameters, Credentials, ShowHideToggle})
    Line 550:            Encoding = CType(results(1),String)
    Line 551:            MimeType = CType(results(2),String)
     

    Source File: C:\Sites\LibertyDEV\ReportAccess\Web References\AppReportService\Reference.vb    Line: 549

    Stack Trace:


    [SoapException: The permissions granted to user 'TIBURON\IUSR_MARLIN' are insufficient for performing this operation. --> The permissions granted to user 'TIBURON\IUSR_MARLIN' are insufficient for performing this operation. --> The permissions granted to user 'TIBURON\IUSR_MARLIN' are insufficient for performing this operation.]
       System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall) +1494
       System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters) +218
       ReportAccess.AppReportService.ReportingService.Render(String Report, String Format, String HistoryID, String DeviceInfo, ParameterValue[] Parameters, DataSourceCredentials[] Credentials, String ShowHideToggle, String& Encoding, String& MimeType, ParameterValue[]& ParametersUsed, Warning[]& Warnings, String[]& StreamIds) in C:\Sites\LibertyDEV\ReportAccess\Web References\AppReportService\Reference.vb:549
       ReportAccess.WebForm1.Page_Load(Object sender, EventArgs e) in C:\Sites\LibertyDEV\ReportAccess\ReportAccessMenu.aspx.vb:103
       System.Web.UI.Control.OnLoad(EventArgs e) +67
       System.Web.UI.Control.LoadRecursive() +35
       System.Web.UI.Page.ProcessRequestMain() +731

     


    --------------------------------------------------------------------------------
    Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET Version:1.1.4322.573

     

     

     

    Thanks,

     

    Sunnergren

    jpvalentin
    Web Export to PDF with SOAP, Forcing Download of PDF: an example
    Posted: Friday, December 17, 2004 3:11 PM (GST)

    Are you sure that IIS virtual directory ReportServer security tab has Integrated Windows Authentification selected ?

    If yes, I don't know why you have got this error, but try to add this in your code and replace username, pwd and domain by your values :

     

       System.Net.ICredentials netCredential =
        new System.Net.NetworkCredential("Username","Password","Domain");
       rs.Credentials = netCredential;

    Remarks : it is a C# syntax, this must replace the Default Credential Cache.

     

    JP VALENTIN

    sunnergren
    Web Export to PDF with SOAP, Forcing Download of PDF: an example
    Posted: Friday, December 17, 2004 5:03 PM (GST)

    Replacing the line about default credentials with . . .

     


            Dim netCredential = New System.Net.NetworkCredential("Username", "xxxxxx", "domainname")


            rs.Credentials = netCredential

     

    worked.

     

    Obviously, I would rather not have to specify the user in my code . . .

     

    -Sunnergren

    jpvalentin
    Web Export to PDF with SOAP, Forcing Download of PDF: an example
    Posted: Monday, December 20, 2004 6:24 AM (GST)

    The last thing I can say is , replace netCredential with DefaultCredential, open a NT session with the username tested above in the netCredential and this will be OK.

     

    JP VALENTIN

    sunnergren
    Web Export to PDF with SOAP, Forcing Download of PDF: an example
    Posted: Monday, December 20, 2004 12:52 PM (GST)

    It is the same userid/password/domain that I have been testing with all along. It appears that "defaultCredentials" is not picking up my userid/password correctly.

     

    Thanks for your help anyway,

     

    -Sunnergren

    forbesn
    Web Export to PDF with SOAP, Forcing Download of PDF: an example
    Posted: Monday, July 14, 2008 9:56 PM (GST)
    Thanks a lot. This works for SQL Reporting Services 2005 as well.  I was able to create a .NET page that I call from my existing asp application.  With this routine and SQL Reporting Services I will be able to replace my existing Access Program and 3rd party PDF Creator.

     
     
     
     
     
    Home | Book | Contents | Preview | Articles | Tools | Newsletters | Premium | Ask | About