Sunday, 2 November 2008

NAV 2009 Web Services and InfoPath

I gave an introductory talk at Christchurch CodeCamp 2008 on Dynamics NAV from a Developer's Perspective yesterday but unfortunately ran out of time before being able to show this example of consuming a page web service in an InfoPath form, so I figured I'd make a blog post.

Lars Lohndorf-Larsen has already made a couple of posts on how to expose codeunits and pages as web services using the upcoming NAV 2009 three-tiered architecture and has provided a sample c# program for consuming them.

I'm using the Marketing Beta Release for this so any partner will be able to download the VPC image and repeat this exercise.

First of all, you'll need to expose the Customer Page (Object ID=21) as a web service with a service name of Customer. This is as simple as running form 810 Web Services and inserting a record with Object Type=Page, Object ID =21, Service Name=Customer and ticking the Published field.

If you enter http://localhost:7047/DynamicsNAV/ws/CRONUS_International_Ltd/Page/Customer in your web browser, you should see the WSDL for the page web service. It still amazes me how easy this is.

Now it's time to start up InfoPath. On the Getting Started page, click the Design a Form Template... option on the left hand pane.
In the Design a Form Template dialog, click the Web Service option and click OK.

Select the option to Receive and submit data and click the Next button. Paste the URL for the Customer Web Service into the field that is asking for the location of the data connection web service. If you used the right URL (I just copied it from my browser address bar after I had checked the WSDL for my web service) you should see the following screen when you click Next.

I found this screen a little confusing because I've never used InfoPath before I didn't realise it is wanting me to tell it which method can be used for reading data from the web service. Select the ReadMultiple option and click Next. On the next screen, you get an opportunity to name the data connection. I left mine as the default Main query and clicked Next. The next screen askes for the URL to the web service that will be used when users submit their changes. It defaults to the previous URL so just click Next. On the following screen you are asked to select the operation that will be used for submitting the data. I selected UpdateMultiple and clicked Next.
On this page we need to specify where the Customer comes from so I clicked the Modify button next to the Field or group text box and drilled down until I could select the Customer Node in the XML structure.
You can leave the name for the data connection as Main submit and click the Finish button. If all went to plan, you should now have a new template to drag your fields on to.
Drag the filter group on to the Query fields area of the design surface and select the option to add the groups as a repeating table. This way users will be able to add any filter fields to the read command. You can select the properties of these fields and set a default value - for example, Field defaults to No. and Criteria defaults to *. This will ensure you get some records back. You will also need the setSize parameter since this is mandatory for the ReadMultiple operation. Drag this on to the control surface and give it a default of 100. You should have a query section that looks something like this.

Now expand the dataFields node and drag the Customer node on to the fields area of the design surface and once again select Repeating Table as the option. This puts in every field which is way too much so just right click on the table after you have created it and selec the properties, then click the Change Binding button. You will see a tree that is expanded to show the customer node, that's fine, just click Next and you'll get on to a dialog where you can remove fields. For a quick way to remove all fields, click in the Columns in table list box and hold down Alt+R until there are no fields left - now you can click on the fields you want and add them. I used No, Name, Address and City. You should re-size the fields so they fit the available space and have a larger Name and Address field. Now it's time to run the form and see what happens. Click the Preview button. When prompted, select the option to connect rather than work offline. Hit the Run Query button and accept the dialog box and you should see something similar to the following.
Nice! OK now you can try editing the name of one of the customers and hitting the submit button. If you see a dialog saying "The form was submitted successfully, you've just updated the NAV data!

14 comments:

Ian Piddington said...

Cool example when I tried it everything appears to work, I get the success message back when submitting my changes but when I look in NAV or re-run the query the changes haven't committed to the database. I am no Infopath expert so I may have missed something in the form.

Maged Soliman said...

Ya, I did follow the same example and noticed that the UpdateMultiple method of the web service ONLY update changes made to the first record on the first Repeating table row. Any changes to the following rows are not updated back to NAV.

Gaspode said...

Hi Maged, thanks for leaving a comment, I really appreciate it. Yes you're right the Web Service with Infopath is really simple but the drag and drop method doesn't update any records other than the first. I am going to try and find out how to do this using configuration and if I cannot, I'll post some code to do it. If anyone else solves this first, please feel free to blog it and post a link here.

Merlin said...

Hey Gaspode! Thanks a lot for your explanation. It works like a charm, I do have some problems while creating an Sales Order or Customer: some Date fields are required in InfoPath but are read-only in NAV. Therefore I cannot submit the record without an error. Any help is much appreciated!

Gaspode said...

Hi Merlin, Hmmm. Maybe you should post a question on dynamicsuser.net or mibuso.com. I have tried to get some help from InfoPath experts but things seem to be a bit slow moving. I'd like to solve the problem of updating all records and not just the first, but this date one is new to me. Are you including non-editable (like Date Last Modified) fields on the InfoPath form?

mootsoo said...

Hi Gaspode
Thanks for this post. It looks nice.
But i have problem to do this.
I have installed full version of NAV2009 and also i have license.
But I couldn't find Form810. And also i couldn't create page in my page designer. Can you tell me why it is?

Gaspode said...

Hi Mootsoo, it seems to me that the license you are using has not been upgraded to work with the RoleTailored client. If you get a newer version of your developer's license you should be fine with this. For this particular example, you can use the CRONUS demo license as we are not writing any code in NAV.

mootsoo said...

Hi Gaspode
Thank you for answering.
Yeah your right. Now i have Form810 :).
But one problem there.
I don't understand that URL. I couldn't connect it. Can you explain that URL?

Gaspode said...

Hi Mootsoo, there is a pretty good explanation of the URL in my book and you are lucky because you can read the section as a free article on the publisher's web site. Take a look at "Where do I find that URL?" in the article http://www.packtpub.com/article/extending-application-using-microsoft-dynamics-nav-2009-part1.

mootsoo said...

Hi Gaspode
Thanks, i have connected.
But also there is a problem. When i doing like this your article, that error comes: WSDLReader:Could not find '/definitions' inside the default WSDL namespace HRESULT=0x80004005: Unspecified error
- WSDLReader:Loading of the WSDL file failed HRESULT=0x80004005: Unspecified error

If i doing in Visual Studio it can't specify namespace Webservice. And also Customer_Service, Customer_Filter - missing assembly reference.

But i can't solve this problem.

Gaspode said...

Hi Mootsoo, Hmmm. I'm not really sure I understand what your problem is. If you enter the URL to your service into Internet Explorer, do you see a valid WSDL file? You need to ensure this is correct first, maybe you are not putting in the correct URL, or maybe you have other problems with the web service. As for the Visual Studio, I'm not sure what you're getting at there. If you are on dynamicsuser.net you can private message me with your e-mail address and I might be able to help more.

mootsoo said...

ok
i have sent you short e-mail.

NicoleB said...

I am unable to browse to the WSDL document. I am new to Navision, just studying for my C/SIDE Intro exam. I can see my web services in my role tailored client, and they are published. Am I missing a step? Thank you for any help you can give a novice.

Gaspode said...

Hi Nicole, make sure your Microsoft Dynamics NAV Business Web Services service is running. If it is, try typing in the URL into Internet explorer. Follow the links at the start of this blog for Lars's basic how to guide on accessing Web services and also see the free article extract from my book on the PACKT web site. You can find links to this from www.teachmenav.com.