Web Services and Forms

Exclusive offer: get 50% off this eBook here
Extending Microsoft Dynamics AX 2012 Cookbook

Extending Microsoft Dynamics AX 2012 Cookbook — Save 50%

A practical guide to extending and maximizing the potential of Dynamics AX using common Microsoft technologies with this book and ebook

£20.99    £10.50
by Murray Fife | September 2013 | Cookbooks Enterprise Articles

In this article created by Murray Fife, the author of Extending Microsoft Dynamics AX 2012 Cookbook, we will show you how you can use Microsoft InfoPath in conjunction with web services linked to Dynamics AX to create smarter forms. You will also learn the following:

  • Creating a Dynamics AX web service
  • Creating a web service wrapper
  • Using a Dynamics AX web service in an InfoPath form
  • Creating custom OData queries to retrieve Dynamics AX data
  • Building InfoPath lookups using OData queries

Web services are a great way for other applications to connect with Dynamics AX, and are also very easy to create and use through the tools that are delivered with Dynamics AX. You can create new web services that you can use to perform updates in the system, and you can also use the standard query services to get data from the database, all through a secure method.

InfoPath is a great Office application that you may already have installed, but not know what you can use it for. It allows you to create forms and documents that can read and update databases, and also use web services to grab and update data. When you combine these two, you will be able to create simpler forms very quickly that the users can use for Dynamics AX without having to be an expert programmer.

In this Article, we will show you how you can use web services and InfoPath to create a simple update form for Dynamics AX.

(For more resources related to this topic, see here.)

Creating a Dynamics AX web service

There are a number of web services that have already been created and deployed with the standard Dynamics AX install. There are a lot more services that you can publish as web services through the AOT in just a matter of minutes, allowing you to access and update almost any area of Dynamics AX from other applications.

In this recipe, we will show you how you can create new web services from within the Dynamics AX development environment.

How to do it...

To create a new web service within Dynamics AX, follow these steps:

  1. From within the AOT explorer, create a new project for the web service.

  2. From inside the project, right-click on the project name and from the New submenu, select Service Group to create a new web service group.
  3. Rename your service group to be something a little more appropriate. In this case, we are creating a sales order web service; so we will rename it as SalesOrderService.
  4. From the AOT browser, open up the Services group, find the service that you want to publish as a web service, and then drag it over onto your new project service group. In this recipe, we selected the SalesSalesOrderService , which has all of the logic to create sales orders.

    You can continue adding as many services into your service group as you like.

  5. When you have finished adding services, right-click on the service group that you created and select the Deploy Service Group menu item. This will process the service group and create a web service for you.

How it works...

To see the web service that was created, open the Inbound ports option from the Services and Application Integration Framework folder of the Setup group in the System administration area page.

Your new service should show up there. If you look at the WSDL URI: field for the inbound port, you will find the URL for the web service itself.

If you browse to that location, you will see the schema for the web service that you will use for other applications to call, in order to update Dynamics AX. For us it's not that user-friendly, but for applications, this is all they need to know.

Creating a web service wrapper

The web services that Dynamics AX creates seem to work best for programming interfaces, and sometimes programs have problems with the format of the web service call. InfoPath is one of these programs. So, we need to wrap the Dynamics AX service within a web service wrapper that InfoPath is able to use. This is not as complicated as it sounds though, and you can quickly do this with Visual Studio.

In this recipe, we will show how you can create a web service wrapper through Microsoft Visual Studio that we can use from within InfoPath.

Getting ready

In order to do this you need to have a copy of Visual Studio. We will be using Visual Studio 2010 in our example, but you should be able to create similar web service wrappers using earlier versions as well.

How to do it...

To create a web service wrapper, follow these steps:

  1. From within Visual Studio, create a new web project and from the template library, select the ASP.NET Web Service Application template.

  2. This will create your web service shell that will be modified to call the Dynamics AX web service. To link the Dynamics AX web service to our project so that we are able to call it, right-click on the References folder in Solution Explorer and select the Add Service Reference... menu item.

  3. From within the Add Service Reference dialog box, paste the URL for your Dynamics AX web service and click on the Go button. This will allow Visual Studio to discover the web service, and you will be able to see all of the operations that are exposed.
  4. Change the name in the Namespace: field to match the web service name so that it will be easier to remember in the later steps, and then click on the OK button.

    When you return to your web service project, you will be able to see the web service reference in the Service References group within Solution Explorer .

  5. Within the header of the web service code, add an entry for your service reference as follows:

    using AXSalesOrderService.SalesOrderServiceReference;

  6. Now, replace the HelloWorld web method code that is added to the web service by default with the following code that will use the web service to create a new sales order:

    [WebMethod] public string NewSalesOrder( string company, string language, string custAccount, string PONumber, string itemID, decimal salesQty, string salesUnit ) { SalesOrderServiceClient client = new SalesOrderServiceClient(); AxdSalesOrder salesOrder = new AxdSalesOrder(); AxdEntity_SalesTable salesTable = new AxdEntity_SalesTable(); AxdEntity_SalesLine salesLine = new AxdEntity_SalesLine(); CallContext callContext = new CallContext(); EntityKey[] keys; EntityKey key; KeyField fld; salesTable.CustAccount = custAccount; salesTable.ReceiptDateRequested = new DateTime(2013, 03, 20); salesLine.ItemId = itemID; salesLine.SalesQty = salesQty; salesLine.SalesUnit = salesUnit; salesTable.SalesLine = new AxdEntity_SalesLine[] { salesLine }; salesTable.PurchOrderFormNum = PONumber; salesTable.SalesType = AxdEnum_SalesType.Sales; salesOrder.SalesTable = new AxdEntity_SalesTable[] { salesTable }; callContext.Company = company; callContext.Language = language; keys = client.create(callContext, salesOrder); key = keys[0]; fld = key.KeyData[0]; return fld.ToString(); }

    You can see this in the following screenshot:

  7. Then, compile your web service.

How it works...

When you compile your program and run it, you will be taken to the web interface for the new web service showing all of the methods that you've exposed.

If you click on the NewSalesOrder web service call, you will be able to see all the parameters that are required to perform the web service.

You can test your web service by filling in the parameters and then clicking on Invoke . This will perform the web service and return with the results of the call.

With a little bit of extra code, you can have the web service return back the order number as well.

To double-check if that everything worked, you can open up Dynamics AX and you should be able to see the new sales order.

Extending Microsoft Dynamics AX 2012 Cookbook A practical guide to extending and maximizing the potential of Dynamics AX using common Microsoft technologies with this book and ebook
Published: August 2013
eBook Price: £20.99
Book Price: £33.99
See more
Select your format and quantity:

Using a Dynamics AX web service in an InfoPath form

InfoPath allows you to quickly create data entry forms that can be saved locally, to SharePoint, and also update data in databases. Additionally, it is able to connect to web services and send and receive data through that channel as well. So once we have a web service wrapper built that links to Dynamics AX, we can create a form that will send information to it in order to add and update data.

In this recipe, we will show how you can create an InfoPath form that uses a Dynamics AX web service wrapper to publish information.

Getting ready

For this recipe, you need to make sure that you have InfoPath installed, since it is usually part of Office Professional Plus or Office 365. Just check that it shows up within the Microsoft Office program group.

How to do it...

To use a web service within an InfoPath form, follow these steps:

  1. Within the InfoPath designer, create a new form and select the Web Service template.

  2. This will automatically open up the Data Connection Wizard . Select the Submit data option, since our example will be sending information to the web service to update Dynamics AX, and then click on Next .

  3. When asked for the web service, type in the URL for the WSDL (Web Services Description Language ) of your web service wrapper and click on Next .

    If you don't know how to find the WSDL, just open up the web service that you are calling from InfoPath, and at the top of the page will be a link for Service Description . If you click on that, it will take you to the WSDL page.

    The URL for this page is the one that you will want to paste into the Web Service: field on the Data Connection Wizard .

  4. If your web service has multiple operations published against it, you will see all of them listed in the next step in the wizard. Select the web service operation that you want your InfoPath form to use when submitting data and click on Next .

  5. Finally, give your data connection a name and click on the Finish button.
  6. Once the data connection is created, the web service parameters will show up as fields within the Fields browser. You can add them to the form individually by dragging and dropping them over, or you can just grab the whole group of fields and drag them onto the form.

  7. To default values in particular fields so that the user doesn't have to type in the values every time, select the Properties menu item after right-clicking on the field in the Fields browser. This will open up the Field or Group Properties window and you can specify the default value in the Value: field.

    In our example, we will default the Company: and Language: fields.

How it works...

To see the form in action, click on the Preview button on the Home ribbon bar.

This will open up the form in edit mode and you can fill in the remaining fields. To send the data to Dynamics AX, click on the Submit button in the Home ribbon bar.

Now, you should be able to see a new order within Dynamics AX that was created by your new InfoPath form.

Creating custom OData queries to retrieve Dynamics AX data

Dynamics AX has a more generic web service call feature called OData Query that allows you to query tables and return them through a URL. This is useful because they can be used as read-only data sources for other programs such as InfoPath.

In this recipe, we will show how you can register your own custom query within Dynamics AX, and then access it through the OData Query web service.

How to do it...

To create an OData query, follow these steps:

  1. To access a query through the OData Query feature, we open the Document data sources form from the Organization Administration area page within the Document Management folder of the Setup group.
  2. To create a new query, click on the New button in the menu bar.
  3. The Document data sources reference the queries that are built within AOT. Usually, you don't have to build a whole new query because you can use one of the existing ones as a basis. Select a module that you would like the data source to be associated with, and then select Custom Query for the data source type. If you just want to query the table with no filter, you could select the Query Reference option, but we want to filter the data before it's sent to us.

  4. In the Data source name field, select the query that you want to publish as an OData Query. In this recipe we want a list of customers; so the CustTableListPage works for us.
  5. On selecting the data source name, AX will open up a query panel, where we can add whatever filters we want, and then we can click on OK to save.

  6. You may want to change the data source name to help you recognize what it is associated with, and then maybe add a description.
  7. Finally, to enable the document data source to be used in the queries, select the Activated checkbox.

    For the following example, we also need to create a second document data source that queries the EcoResProductListPage , to return back all of the products in the database.

How it works...

Now that the query is published as a document data source, you can access it through a URL with this format: http://localhost:8101/DynamicsAx/Services/ODataQueryService/CustTableListPage_US01.

It will return back all of the data.

Building InfoPath lookups using OData queries

A feature of InfoPath that makes forms even more useful is the ability to allow fields to be populated with dynamic data coming from static lists, databases, and also web data sources, so that users do not have to remember field values such as part codes and customer numbers. Since you are able to query Dynamics AX data through web queries, we can use these queries to create dynamic lookups in our forms.

In this recipe, we will show how you can turn text fields into drop-down lists that use OData queries as a data source.

How to do it...

To use an OData query as a data source for a field, follow these steps:

  1. We need to first define the data source. To do this, select the From XML File option in the From Other Sources menu in the Get External Data group of the Data ribbon bar in the form designer.

  2. When the Data Connection Wizard pops up, paste the URL for the OData query that you want to use as a data source and click on Next .

  3. To store the data source with the form, select the Include… option from the data source location section and click on Next .
  4. Finally, give your data source a name and click on Finish .
  5. To use the data source within a field, first you need to change the field's control type to one that will show the data. To do this, right-click on the field and select the Drop-Down List Box option from the Change Control submenu.

  6. Once the control has been changed, right-click on the field again and select the Drop-Down List Properties option.
  7. Change the List box choice from Enter choice manually to Get choice from an external data source and from the Data source dropdown, you will be able to find the XML data source that you just created.
  8. To specify what data is shown in the drop-down box, click on the tree navigation icon to the right of the Entries field. When the XML tree navigator is displayed, find the content node and select it. Selecting the content node will make our drop-down box filter out the metadata information in the XML file that is returned from the OData query, so that we can see all the real records.

  9. Next, click on the tree navigation icon to the right of the Value: and Display name: fields and select the fields that you want to store in the form, and also to be displayed in the dropdown. This will open up the XML tree navigator again and you can select any of the fields from the query.
  10. Finally, you may want to select the Show only entries with unique display names checkbox to filter out any duplicates, and then click on OK .

How it works...

Now when you preview the form, all the customer information will be populated for you to pick from.

By changing the layout of the InfoPath form to have a little bit of a larger font, adding some formatting columns to the body of the form, changing the drop-down fields to list selections, and adding a submit image, we can create a kiosk form that is populated from Dynamics AX, and also create sales orders based on the selections.

Summary

InfoPath is an incredibly useful tool for creating forms and gathering information. When you use it in conjunction with Dynamics AX and just a little bit of coding, it becomes even more useful because you are able to create forms that feed back into the database.

In addition to what we showed you in this Article, you can also:

  • Publish your forms to a SharePoint Forms repository allowing users to access the latest form templates from a centralized location. If you change the template on SharePoint, the users' local copies will also be upgraded ensuring that they always have the latest version.
  • Host the InfoPath forms on a SharePoint site, allowing users to fill in the forms without even having InfoPath installed. This allows you to create forms that customers, vendors, or employees could fill out that could update Dynamics AX.
  • Capture signatures through pen-based input devices such as tablets and Surface devices. The signatures can be stored as JPEG files and even posted to the Dynamics AX attachments if you are clever enough.
  • Publish the InfoPath forms to SharePoint rather than Dynamics AX, while still indexing the document against the key Dynamics AX fields. These document libraries could be linked to records just like the traditional file document libraries that were shown in the earlier Articles.

Other ideas on where you may want to capture information through InfoPath forms could include:

  • Sales people using them to capture store survey information. Pictures from Surface devices could be added to the InfoPath form data as image attachments.
  • Logging of quality issues as Cases within Dynamics AX by mobile users without having to log in to be tethered to a normal PC.
  • Capturing lead and prospect information through a simple table-based form.
  • Simple inquires such as customer details through web-based forms.

Who would have guessed that such an overlooked product could be so useful!

Resources for Article :

 


Further resources on this subject:


Extending Microsoft Dynamics AX 2012 Cookbook A practical guide to extending and maximizing the potential of Dynamics AX using common Microsoft technologies with this book and ebook
Published: August 2013
eBook Price: £20.99
Book Price: £33.99
See more
Select your format and quantity:

About the Author :


Murray Fife

Murray Fife is a Microsoft Dynamics AX MVP, a presenter, and an author with over 18 years of experience in the software industry.

Like most people he has paid his dues as a developer, as an implementation consultant, and a trainer. He has a hard-to-find blend of technical and interpersonal skills and spends his days working with companies solving their problems with the Microsoft suite of products, specializing in Dynamics AX solutions.

Books From Packt


Developing Microsoft Dynamics GP Business Applications
Developing Microsoft Dynamics GP Business Applications

Microsoft Dynamics CRM 2011: Dashboards Cookbook
Microsoft Dynamics CRM 2011: Dashboards Cookbook

Microsoft Dynamics CRM 2011 New Features
Microsoft Dynamics CRM 2011 New Features

Microsoft Dynamics GP 2010 Reporting
Microsoft Dynamics GP 2010 Reporting

Microsoft Dynamics CRM 2011 Application Design
Microsoft Dynamics CRM 2011 Application Design

Microsoft Windows Server AppFabric Cookbook
Microsoft Windows Server AppFabric Cookbook

Microsoft System Center 2012 Service Manager Cookbook
Microsoft System Center 2012 Service Manager Cookbook

Microsoft Dynamics CRM 2011 Cookbook
Microsoft Dynamics CRM 2011 Cookbook


Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software