Consume AX Web Services allows the interaction with AX Data (reading, inserting new data,…) from an external program (web/mobile/desktop applications).

In this post, we are going to create a desktop application with Microsoft Visual Studio 2010 that will interact with our data in Dynamics AX 2009.

Here the necessary steps:

  1. Create a Windows Form Project:
    VisualStudio_NewDLLProject_thumb_5A3D55AC
  2. Add a Web Service Reference to the project:
    image_thumb_6_1331962Bimage_thumb_7_7E4013B7
    This will add to our project the ItemService Reference in the Service Reference Node:
    image_thumb_8_7E4013B7
    And will modify the app.config file:
    image_thumb_11_7E4013B7
  3. Add the Web Service as a new using statement (ProjectName.ServiceName):
    image_thumb_9_7E4013B7
  4. Edit the form with the needed controls (as in the example, if the Web Service needs authentication, you can add fields to request the information):
    image_thumb_10_7E4013B7
    Add the Data Source in the Grid:
    image_thumb_18_7E4013B7
    image_thumb_13_7E4013B7
    If we want the table InventTable as Data Source, we have to change the Data Source Again:
    image_thumb_14_7E4013B7
    Now, it’s time to select the columns that we want to show:
    image_thumb_15_2C2D6670
    image_thumb_16_2C2D6670
    image_thumb_19_2C2D6670
  5. Generate the code to read the information from the InventTable when the button “Read Items” was clicked:Control Names used:
    image_thumb_2C2D6670

    1. Methods for connect with the Web Service and retrieve the data:
      private BindingList<AxdEntity_InventTable> getData()
      {  ItemServiceClient client = new ItemServiceClient
                              ("BasicHttpBinding_ItemService");
          AxdItem item = new AxdItem();
          AxdEntity_InventTable inventTable;
          QueryCriteria query = new QueryCriteria();
          System.Collections.IEnumerator enumeratorInventTable;
          BindingList<AxdEntity_InventTable> collection = new 
                         BindingList<AxdEntity_InventTable>();
      
          client.ClientCredentials.Windows.ClientCredential = new 
          System.Net.NetworkCredential(txtUser.Text, txtPwd.Text, 
          txtDomain.Text);            
                  
          client.Open();
          if (client == null)
          {
              throw new Exception("Cannot instantiate service");
          }
      
          query = PrepareGrid_QueryCriteria();
          item = client.find(query);
      
          enumeratorInventTable = item.InventTable.GetEnumerator();
          while (enumeratorInventTable.MoveNext())
          {
              inventTable = (AxdEntity_InventTable)enumeratorInventTable.Current;
              collection.Add(inventTable);
          }
      
          client.Close();
          return collection;
      }
      
      protected QueryCriteria PrepareGrid_QueryCriteria()
      {
          QueryCriteria queryCriteria;
          CriteriaElement[] criteriaElements;
              
          criteriaElements = new CriteriaElement[1];
          criteriaElements[0] = new CriteriaElement();
      
          criteriaElements[0].DataSourceName = "InventTable";
          criteriaElements[0].FieldName = "ItemId";
          criteriaElements[0].Operator = Operator.Range;
          criteriaElements[0].Value1 = "ITEM-000000";
          criteriaElements[0].Value2 = "ITEM-000010";
      
          queryCriteria = new QueryCriteria();
          queryCriteria.CriteriaElement = criteriaElements;
          return queryCriteria;
      }
    2. Button method to execute the methods above:
      private void btReadItems_Click(object sender, EventArgs e)
      {
          if (txtUser.Text == "" || txtPwd.Text == "")
          {
              throw new Exception("Please fill User and Password");
          }
          dgItems.DataSource = getData(); 
      }
  6. Now, we can execute our application:
    image_thumb_1_2C2D6670 image_thumb_3_2C2D6670
    Filling the information requested and clicking the button “Read Items”, our application should shown the items requested:
    image_thumb_4_2C2D6670