How tos

How to use PowerShell to test your NAV-webservice?

Author
Alain Krikilion (alias kriki)
Website
http://mibuso.com/blogs/kriki
Date
06/03/2015
Size
26,19 KB
Downloads
1560
Rating
54321
Popularity
Downloaded 8 times in the last two weeks
You just created an XML-port and used a Codeunit to publish your XML-port as a webservice and now you want to test to see if it works correctly.
You can write a dedicated C# program, but you spend some time doing it and if your XML-port changes, you have to re-write your code again. I've done this but is not really fun to do.
You can use SoapUI (http://www.soapui.org/) to test it. I did this in the beginning but it is not really easy to use and for some reason, the credentials didn't always work. Probably knowing well how to use it could solve all my problems. But it takes time and I want to have something easier.

Something out-of-the-box

That is the reason I looked to PowerShell and I found it very easy to do. Very easy to change.

How to install a webservice, creating a WSDL-file and using it in a C# project is out of scope of this HowTo. It is about using Powershell!

Let's see step by step how we can do this
  • We have a fob and in it we have these objects:
    • 1 Table. This table is used to store the data we received from PowerShell. It has an auto-incrementing integer that is used as primary key.
    • 1 Page. This page works is used to view the data we received from PowerShell. It is also possible to insert data into the table using this page. This is useful to manually create some records.
    • 1 XML-port. This XML-port can be run stand-alone to import and export data. This is useful to export some records to a file to see the formatting of the XML. This file can then be used in the PowerShell script.
    • 1 Codeunit. This codeunit must be published as a webservice. The codeunit has different methods to be called by PowerShell to play with it. It is also possible to call the webservice WITHOUT XML-structured data. The first few functions in the codeunit are like that to have an easy start.
      • Functions:
        • CreateCode (parameter : Code20) : Creates a record with field "The Code" filled in
        • CreateText (parameter : Text) : Creates a record with field "The Text" filled in (truncated!)
        • CreateDate (parameter : Date) : Creates a record with field "The Date" filled in
        • CreateTime (parameter : Time) : Creates a record with field "The Time" filled in
        • CreateDateTime (parameter : DateTime) : Creates a record with field "The DateTime" filled in
        • CreateInteger (parameter : Integer) : Creates a record with field "The Integer" filled in
        • CreateDecimal (parameter : Decimal) : Creates a record with field "The Decimal" filled in
        • CreateBoolean (parameter : Boolean) : Creates a record with field "The Boolean" filled in
        • GetSomeRandomValue (reference parameter: Text) : returns a 'random' value as a parameter. It returns 'RANDOM ' + the current datetime.
        • GetSomeRandomReturnValue() : Text : returns a random value as a return-value
        • GetWebServiceTestText(reference parameter : Text): returns an XML-text created by all the records in the table via the parameter.
        • GetWebServiceTestTextReturnValue() : text: returns an XML-text created by all the records in the table via the return-value.
        • CreateWebServiceTestText(parameter : text) :call this function with a text that contains the XML-data (as an example you can use the text from the file exported by running the XML-port from the object designer)
        • CreateWebServiceTestXML(parameter : XML-port): Like previous function but CANNOT be used with POSH. You can use it with in C# after creating the wsdl-file. (I have NOT tested this)
        • GetWebServiceTestXML(reference parameter : Text): returns an XML created by all the records in the table via the parameter. CANNOT be used with POSH. You can use it with in C# after creating the wsdl-file. (I have NOT tested this)
  • So import the .fob (it is made with NAV2013R2, but you can also import the .txt-file in NAV2013 and compile it)
  • Define the webservice in page "Web Services":
    • Object Type=Codeunit
    • Object ID=60000
    • Service Name=WEBSERVICETEST
    • Published=Yes
    • If you have NAV2013R2 or later, field "SOAP URL" contains the url to use for the calling the webservice
  • Open the file WEBSERVICETEST.PS1 in Windows PowerShell ISE.
  • In the file, you can run the different lines 1 by 1 and see the result in the RTC.


Some extra's

You can also use this functionality in combination with SQL Server Agent Jobs (Or Windows Scheduler) to launch a webservice that launches certain functionalities instead of using jobqueues. I do have some customers with NAV2009R2 in which I use this system. Jobqueue is still limited to 1 function at the time. Using this system I can run multiple jobs in parallel.
Download code

Screenshots