CONVERT QUERYEXPRESSION TO FETCHXML AND VICE-VERSA IN DYNAMICS 365
In this post, you will learn about converting Query Expression to FetchXML and vice versa in Dynamics 365. Dynamics 365 is a powerful tool for managing and analyzing data, and the ability to convert Query Expressions to FetchXML and vice versa allows for more flexibility in data analysis.
How to Convert Query Expression to FetchXML
-
QUERY EXPRESSION TO FETCH XML
The following function can be used to convert a Query Expression into a FetchXML query.
public void ConvertQueryExpressionToFetchXml()
{
List<Account> lstAccounts = new List<Account>();
QueryExpression qeQuery = new QueryExpression(Account.EntityLogicalName)
{
ColumnSet = new ColumnSet(Account.Fields.Id),
Criteria = new FilterExpression()
{
FilterOperator = LogicalOperator.And,
Conditions =
{
new ConditionExpression(Account.Fields.StateCode, ConditionOperator.Equal, (int)AccountState.Active),
}
},
};
var qeToFetchXmlRequest = new QueryExpressionToFetchXmlRequest
{
Query = qeQuery
};
var qeToFetchXmlResponse = (QueryExpressionToFetchXmlResponse)AdminService.Execute(qeToFetchXmlRequest);
var fetchXml = qeToFetchXmlResponse.FetchXml;
}
-
FETCH XML TO QUERY EXPRESSION
The following function can be used to convert a FetchXML query into a Query Expression.
public void ConvertFetchXmlToQueryExpression()
{
List<Account> lstAccounts = new List<Account>();
FetchExpression feQuery = new FetchExpression();
feQuery.Query = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='account'>
<attribute name='name' />
<attribute name='primarycontactid' />
<attribute name='telephone1' />
<attribute name='accountid' />
<order attribute='name' descending='false' />
<filter type='and'>
<condition attribute='statecode' operator='eq' value='0' />
</filter>
</entity>
</fetch>";
var feToQueryExpressionRequest = new FetchXmlToQueryExpressionRequest
{
FetchXml = feQuery.Query
};
var feToQueryExpressionResponse = (FetchXmlToQueryExpressionResponse)AdminService.Execute(feToQueryExpressionRequest);
var queryExpression = feToQueryExpressionResponse.Query;
}
This tutorial provides step-by-step instructions on how to accomplish these conversions, with code snippets and visuals to aid the process. With this knowledge at your disposal, you will be better equipped to manipulate and analyze data with greater finesse and flexibility.
Link to the original post - here.
Published on:
Learn moreRelated posts
Get Related Entity data along with primary entity data in MS Dynamics CRM using QueryExpression
If you're looking to enhance the functionality of your MS Dynamics CRM by accessing related entity data along with primary entity data, this t...
Query Data Using Web API
If you're looking to leverage web APIs to query data, this post is a great place to start. In this article, you'll explore how to use web APIs...
Use of aggregate, groupby in fetchxml query
This post dives into the use of aggregate and groupby functions in FetchXML queries. By utilizing these functions, users can create more compl...
How to get direct count using Fetch XML capabilities
Explore the powerful capabilities of FetchXML in Dynamics 365 with this informative post. As a query language, FetchXML retrieves data from th...
Data migration tools and services for Dynamics 365 CRM
For those adopting Microsoft Dynamics CRM, data migration is a necessary and significant step in the process. Regardless of whether you're a n...
Data Migration Services for Cloud Storage Company's Dynamics 365 CRM 8.2 to 9.1 Upgrade
This post on CRM Software Blog talks about the challenges associated with migrating from Microsoft Dynamics 365 CRM (On-Premises) 8.2 to 9.1, ...
Datatype Conversion in Power Query Affects Data Modeling in Power BI
Are you a Power BI developer struggling with data type issues? Look no further. In this blog post, the common challenges arising from inapprop...
DYNAMICS 365 HOW TO EXECUTE FETCHXML QUERIES IN WEB.API JAVASCRIPT
If you're working with Dynamics 365 and require retrieving data through WebAPI calls, FetchXml might be the best option for more complex queri...
DYNAMICS 365 HOW TO EXECUTE FETCHXML QUERIES IN C#
If you're a Dynamics 365 user looking to execute FetchXml queries in C#, this quick post has got you covered. First, you need to prepare the F...