5 min read

(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.

Subscribe to the weekly Packt Hub newsletter

* indicates required


Please enter your comment!
Please enter your name here