Fetching data from AX table using Query AIF service Dynamics Ax 2012 R3

We can expose AOT query as service which can consume in any WCF client. We can call and fetch data from any AX table,  without Static AOT query. For this purpose Dynamics Ax provide the methods to create a query at run time and call with Query service.


This Query service is built in feature if during Application Interface framework component installed with Dynamics Ax Setup.



using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Data;

//using Microsoft.Dynamics.AX.Framework.Services.Metadata.Contracts;




namespace ConsoleApplication1


class Program


static void Main(string[] args)


testService.QueryServiceClient _QueryClient = new testService.QueryServiceClient();

DataSet dataSet;

testService.Paging paging = null;

testService.QueryMetadata query;

testService.QueryDataSourceMetadata customerDataSource;


query = new testService.QueryMetadata();


// Set the properties of the query.

query.QueryType = testService.QueryType.Join;

query.AllowCrossCompany = true;

query.DataSources = new testService.QueryDataSourceMetadata[1];


// Set the properties of the Customers data source.

customerDataSource = new testService.QueryDataSourceMetadata();

customerDataSource.Name = "DataArea";

customerDataSource.Enabled = true;

customerDataSource.FetchMode = testService.FetchMode.OneToOne;

customerDataSource.Table = "DataArea";

// Setting DynamicFieldList property to true returns all fields.

customerDataSource.DynamicFieldList = true;

//Add the data source to the query.

query.DataSources[0] = customerDataSource;


dataSet = _QueryClient.ExecuteQuery(query, ref paging);

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













if you got error with similar message “The maximum message size quota for incoming messages (65536) has been exceeded. To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element.

update the binding section of app.config

<binding name=”QueryServiceEndpoint” transferMode=”Streamed”
<readerQuotas maxDepth=”32″



Ali Raza Zaidi, Microsoft ERP Consultant, Currently working for Business Experts Gulf in United Arab Emirates. Specialized in HR & Payroll, Supply Chain and Financials modules.