QueryExpressionToFetchXmlRequest Class


Applies To: Dynamics 365 (online), Dynamics 365 (on-premises), Dynamics CRM 2016, Dynamics CRM Online

Contains the data that is needed to convert a query, which is represented as a QueryExpression class, to its equivalent query, which is represented as FetchXML.

For the Web API use the QueryExpressionToFetchXml Action .

Web API support for this message was added with December 2016 update for Microsoft Dynamics 365 (online) and December 2016 Service Pack for Microsoft Dynamics 365 (on-premises) .

Namespace:   Microsoft.Crm.Sdk.Messages
Assembly:  Microsoft.Crm.Sdk.Proxy (in Microsoft.Crm.Sdk.Proxy.dll)

Inheritance Hierarchy



[DataContractAttribute(Namespace = "http://schemas.microsoft.com/crm/2011/Contracts")]
public sealed class QueryExpressionToFetchXmlRequest : OrganizationRequest
<DataContractAttribute(Namespace := "http://schemas.microsoft.com/crm/2011/Contracts")>
Public NotInheritable Class QueryExpressionToFetchXmlRequest
    Inherits OrganizationRequest


Name Description
System_CAPS_pubmethod QueryExpressionToFetchXmlRequest()

Initializes a new instance of the the QueryExpressionToFetchXmlRequest class.


Name Description
System_CAPS_pubproperty ExtensionData

Gets or sets the structure that contains extra data. Optional.(Inherited from OrganizationRequest.)

System_CAPS_pubproperty Item[String]

Gets or sets the indexer for the Parameters collection.(Inherited from OrganizationRequest.)

System_CAPS_pubproperty Parameters

Gets or sets the collection of parameters for the request. Required, but is supplied by derived classes.(Inherited from OrganizationRequest.)

System_CAPS_pubproperty Query

Gets or sets the query to convert.

System_CAPS_pubproperty RequestId

Gets or sets the ID of an asynchronous operation (system job). Optional. (Inherited from OrganizationRequest.)

System_CAPS_pubproperty RequestName

Gets or sets the name of the request. Required, but is supplied by derived classes.(Inherited from OrganizationRequest.)


Name Description
System_CAPS_pubmethod Equals(Object)

(Inherited from Object.)

System_CAPS_pubmethod GetHashCode()

(Inherited from Object.)

System_CAPS_pubmethod GetType()

(Inherited from Object.)

System_CAPS_pubmethod ToString()

(Inherited from Object.)


Message Availability

This message works regardless whether the caller is connected to the server or offline.


Pass an instance of this class to the Execute method, which returns an instance of the QueryExpressionToFetchXmlResponse class.

Privileges and Access Rights

For a complete list of the required privileges, see QueryExpressionToFetchXml message privileges.

Notes for Callers

Use of the keyword distinct in query expressions is different from the FetchXML query language. When you map a query from Fetch to QueryExpression, you receive inconsistent results if you use the Distinct keyword in the FetchXML string.


The following example shows how to use this message. For this sample to work correctly, you must be connected to the server to get an IOrganizationService interface. For the complete sample, see the link later in this topic.

// Build a query expression that we will turn into FetchXML.
var queryExpression = new QueryExpression()
    Distinct = false,
    EntityName = Contact.EntityLogicalName,
    ColumnSet = new ColumnSet("fullname", "address1_telephone1"),
    LinkEntities = 
            new LinkEntity 
                JoinOperator = JoinOperator.LeftOuter,
                LinkFromAttributeName = "parentcustomerid",
                LinkFromEntityName = Contact.EntityLogicalName,
                LinkToAttributeName = "accountid",
                LinkToEntityName = Account.EntityLogicalName,
                LinkCriteria = 
                    Conditions = 
                        new ConditionExpression("name", ConditionOperator.Equal, "Litware, Inc.")
    Criteria =
        Filters = 
                new FilterExpression
                    FilterOperator = LogicalOperator.And,
                    Conditions = 
                        new ConditionExpression("address1_stateorprovince", ConditionOperator.Equal, "WA"),
                        new ConditionExpression("address1_city", ConditionOperator.In, new String[] {"Redmond", "Bellevue" , "Kirkland", "Seattle"}),
                        new ConditionExpression("createdon", ConditionOperator.LastXDays, 30),
                        new ConditionExpression("emailaddress1", ConditionOperator.NotNull)
                new FilterExpression
                    FilterOperator = LogicalOperator.Or,
                    Conditions =
                        new ConditionExpression("address1_telephone1", ConditionOperator.Like, "(206)%"),
                        new ConditionExpression("address1_telephone1", ConditionOperator.Like, "(425)%")

// Run the query as a query expression.
EntityCollection queryExpressionResult =
Console.WriteLine("Output for query as QueryExpression:");

// Convert the query expression to FetchXML.
var conversionRequest = new QueryExpressionToFetchXmlRequest
    Query = queryExpression
var conversionResponse =

// Use the converted query to make a retrieve multiple request to Microsoft Dynamics CRM.
String fetchXml = conversionResponse.FetchXml;
var fetchQuery = new FetchExpression(fetchXml);
EntityCollection result = _serviceProxy.RetrieveMultiple(fetchQuery);

// Display the results.
Console.WriteLine("\nOutput for query after conversion to FetchXML:");

         ' Build a query expression that we will turn into FetchXML.
         Dim queryExpressionObj As QueryExpression =
             New QueryExpression() With
                 .Distinct = False,
                 .EntityName = Contact.EntityLogicalName,
                 .ColumnSet = New ColumnSet("fullname", "address1_telephone1")
         Dim queryLink As New LinkEntity() With
                 .JoinOperator = JoinOperator.LeftOuter,
                 .LinkFromAttributeName = "parentcustomerid",
                 .LinkFromEntityName = Contact.EntityLogicalName,
                 .LinkToAttributeName = "accountid",
                 .LinkToEntityName = Account.EntityLogicalName
                                               "Litware, Inc.")

         Dim queryCriteriaFilter1 As New FilterExpression() With
                 .FilterOperator = LogicalOperator.And
             New ConditionExpression("address1_stateorprovince",
             New ConditionExpression("address1_city",
                                     New String() {"Redmond",
             New ConditionExpression("createdon",
             New ConditionExpression("emailaddress1",
         Dim queryCriteriaFilter2 As New FilterExpression() With
                 .FilterOperator = LogicalOperator.Or
             New ConditionExpression("address1_telephone1",
             New ConditionExpression("address1_telephone1",

         ' Run the query as a query expression.
         Dim queryExpressionResult As EntityCollection =
         Console.WriteLine("Output for query as QueryExpression:")

         ' Convert the query expression to FetchXML.
         Dim conversionRequest As QueryExpressionToFetchXmlRequest =
             New QueryExpressionToFetchXmlRequest With {.Query = queryExpressionObj}
         Dim conversionResponse = CType(_serviceProxy.Execute(conversionRequest), 

         ' Use the converted query to make a retrieve multiple request to Microsoft Dynamics CRM.
Dim fetchXml As String = conversionResponse.FetchXml
Dim fetchQuery = New FetchExpression(fetchXml)
Dim result As EntityCollection = _serviceProxy.RetrieveMultiple(fetchQuery)

' Display the results.
Console.WriteLine(vbLf &amp; "Output for query after conversion to FetchXML:")

Thread Safety

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Microsoft.Crm.Sdk.Messages Namespace
Retrieve data with queries using SDK assemblies
Page large result sets with QueryExpression
Build queries with QueryExpression
FetchXML schema
Sample: Convert queries between Fetch and QueryExpression

Return to top

Microsoft Dynamics 365

© 2016 Microsoft. All rights reserved. Copyright