Skip to main content



No record found.

Microsoft Dynamics AX forum

Sort, range not working - fetching data from AX table using Query AIF service

Posted on by 130


I' am successfully retrieving data from Microsft Dynamics Ax2012 via AIF document services. However, it shows me all the records with no filters applied and not sorted. I didn't get any error message.

C# Example:

public async Task GetCustTableByCurrencyCodeAsync(string currencyCode)
    QueryResult queryResult;
    ExecuteQueryResponse executeQueryResponse;

        var customerDataSource = new QueryDataSourceMetadata
            Table = "CustTable",
            Name = "CustTable",
            HasRelations = false,
            Enabled = true,
            DynamicFieldList = true,
            EnabledSpecified = true,
            FetchModeSpecified = true,
            DynamicFieldListSpecified = true,
            OrderModeSpecified = true

        var range = new QueryDataRangeMetadata
            TableName = "CustTable",
            FieldName = "Currency",
            Value = currencyCode,
            Enabled = true

        customerDataSource.Ranges = new QueryRangeMetadata[] { range };

        var sort = new QueryDataOrderByMetadata
            DataSource = "CustTable",
            FieldName = "AccountNum",
            SortOrder = SortOrder.Descending

        var query = new QueryMetadata
            QueryType = QueryType.Join,
            DataSources = new[] { customerDataSource },
            OrderByFields = new QueryOrderByMetadata[] { sort }

        var paging = new PositionBasedPaging
            StartingPosition = 1,
            NumberOfRecordsToFetch = 10,
            NumberOfRecordsToFetchSpecified = true,
            StartingPositionSpecified = true

        ExecuteQueryRequest req = new ExecuteQueryRequest(query, paging);

        using (var client = ClientFactory.CreateClient())
            executeQueryResponse = await client.ExecuteQueryAsync(req);

        queryResult = createQueryResponse(ToDataSet(executeQueryResponse.ExecuteQueryResult));

    catch (Exception ex)
        throw new FaultException(ex.Message);

    return queryResult;

Why is sort and filter not working?

Thank you in advance,

  • Martin Dráb Profile Picture
    Martin Dráb 225,610 Super User on at
    RE: Sort, range not working - fetching data from AX table using Query AIF service

    You seem to be using the Query service, not an AIF document service.

    Unfortunately I don't see anything wrong in your code, therefore I don't have a solution for you.

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .


André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,995 Super User

Martin Dráb Profile Picture

Martin Dráb 225,610 Super User

nmaenpaa Profile Picture

nmaenpaa 101,148


Featured topics

Product updates

Dynamics 365 release plans