Microsoft Dynamics AX 2012 comes with a pre-installed, all important, AIF query service. Query service can be used to fetch ad hoc data from AX2012 without creating a service. There are three types of queries that you can run through a query service but I am discussing only the first two options here:

  • Static query: use this if you require re-using the fetch query. This mod requires you to create the AOT query first.
  • User defined query: use this mod if you don’t want to create the AOT query. In this mod, QueryMetaData object is been used to build the query at runtime.

In the below mentioned code snippet, I am trying to fetch all the records from the HcmWorker table through the user defined query. In order to run this snippet, please make sure that AIF services are deployed and you are able to access the query service. Typical URI for the query service WSDL is:

http://<AOS_HOST>:8101/DynamicsAx/Services/QueryService

The code snippet to fetch all the records from HcmWorker is:

QueryServiceClient client = new QueryServiceClient();

QueryMetadata queryMetaData = new QueryMetadata();

queryMetaData.DataSources = new QueryDataSourceMetadata[] {

new QueryDataSourceMetadata() { DynamicFieldList=true, Table=”HcmWorker”,  Name=”HcmWorker”,  Enabled=true}

};

Paging paging = null;

 

DataSet ds = client.ExecuteQuery(queryMetaData, ref paging);

foreach (DataRow dr in ds.Tables[0].Rows)

{

Console.WriteLine(dr[“PersonnelNumber”].ToString());

}

 

Console.ReadLine();

Happy DAXing 🙂

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Comments

    Archives

    Categories