How to get a Dynamics NAV report with a Web Service

1. Create a new codeunit. In this scenario we will call this codeunit "CUWebReport" with ID 50000

2. Navigate to "C/AL Globals" and create a function called "GenerateReport"

image 

3. Select "Locals"

4. Select the "Return Value" tab

5. Set "Return Type"=Text and "Length"=100

image

6. With this completed close "C/AL Locals" window.

7. Now with "C/AL Globals" windows active again. Select "Variables" tab.

8. Create a variable called "filename" with "Data Type"=Text and "Length"=100

image

9. Now let's add the following code to this codeunit:

filename := 'C:\inetpub\PdfDocuments\';
filename += FORMAT(CREATEGUID);
filename := DELCHR(filename, '=', '{-}');
filename += '.pdf';
REPORT.SAVEASPDF(111,filename);
EXIT(filename);

image

10. Save and compile the codeunit.

11. Now it's time to expose this codeunit as Web Service. Navigate to "Administration/IT Administration/General Setup/Web Services"

12. Select codeunit 50000 and give this a service name, we use "Get_PDF_Report"

image

13. Now it is time to verify that we can see this web service. Open this URL  https://localhost:7047/DynamicsNAV/WS/services.

You should now se this message in your browser, and your Web Service can now be called :

image

If you don't see this message, you might want to check that  the service "Microsoft Dynamics NAV Business Web Services" has been started.

14. Now it is time to call the Web Service, in this example we use Microsoft Visual Web Developer 2005. And we use the following code to call the Web Service:

Partial Class _Default
Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim service As localhost.Get_PDF_Report = New localhost.Get_PDF_Report()
service.UseDefaultCredentials = True
service.Url = "
https://localhost:7047/DynamicsNAV/WS/CRONUS_International_Ltd/Codeunit/Get_PDF_Report"
        Response.ContentType = "application/pdf"
Dim pdfFileName As String = service.GenerateReport()
Response.TransmitFile(pdfFileName)

    End Sub
End Class

But how to consume this Web Service is not in scope for this blog, so we suggest you have look our online help how to consume a Web Service from NAV 2009.

Online help found here: https://msdn.microsoft.com/en-us/library/dd339004.aspx

15. Now also remember to impersonate your web servers application to an appropriate Dynamics NAV user.

16. After compiling a running our project we then get this button in our browser.

image

17. When activating this button, we get the report specified in Codeunit 50000 displayed as an PDF file in our browser.

image

Conclusion, now you can give this URL to people who don't have access to Dynamics NAV, and they can execute the report when they see a need for this.

Thanks,

Torben Meyhoff, SDE & Claus Lundstrøm, Program Manager, Microsoft Dynamics NAV

Comments

  • Anonymous
    August 14, 2009
    Torben The only problem with this sample is the possibility to submit the pdf through the web service. In a secure world you don't have access to that part of the file system. What is solution for this problem...

  • Anonymous
    August 16, 2009
    Hi Chiel File system was used in this example to make it as simple as possible. You can also use a database as store for your PDF files if that is more suited for your scenario. Best regards, Torben

  • Anonymous
    August 20, 2009
    Hi this is great article and works very well with standard reports. But I am trying to generate pdf for customized reports with the help of webservice. Though saveaspdf is better approach, but since I am not known to RTC layout of the nav classic report(RDLC), I am simply trying to use the printing to pdf printers like bullzip or pdfcreator. I can do it from within the codeunit in nav alright, but webservice is not able to utilize this codeunit function to print the pdf. What I have found is that the problem is in Report.Runmodal or Report.Run function. I am getting soapexception of callback functions are not allowed. Is it not possible to print reports to default printer or pdfprinter through codeunit to be consumed by .net application, or do I miss some settings or making any mistakes? Thanks in advance, Rushabh.