Convert QueryExpression to FetchXML and FetchXML to QueryExpression in MSCRM

It might a very common requirement that sometimes you need to convert FetchXMl to QueryExpression and Vice-Versa. Today i am going to share the same with all of you.

Query Expression to Fetch XML :

QueryExpression query = new QueryExpression(); 
            query.EntityName = \”contact\”;
            query.ColumnSet = new ColumnSet(true);
            ConditionExpression cond = new ConditionExpression();
            cond.AttributeName = \”firstname\”;
            cond.Operator = ConditionOperator.Equal;
            cond.Values.Add(\”Varun Singh\”);
            // Create Request
            QueryExpressionToFetchXmlRequest Queryexp_to_Fetch_req = new QueryExpressionToFetchXmlRequest();
            Queryexp_to_Fetch_req.Query = query;
            //Get Response
            QueryExpressionToFetchXmlResponse Queryexp_to_Fetch_resp = (QueryExpressionToFetchXmlResponse)service.Execute(Queryexp_to_Fetch_req);
            //work with newly formed fetch string
            string myfetch = Queryexp_to_Fetch_resp.FetchXml;

Fetch XML to Query Expression :

FetchXmlToQueryExpressionRequest fetch_to_QueryExp_req = new FetchXmlToQueryExpressionRequest();
            fetch_to_QueryExp_req.FetchXml = myfetch;
            FetchXmlToQueryExpressionResponse fetch_to_QueryExp_resp = (FetchXmlToQueryExpressionResponse)service.Execute(fetch_to_QueryExp_req);
            QueryExpression myquery = fetch_to_QueryExp_resp.Query;
Hope it will help someone……..

Published by arpitpowerguide

My name is Arpit Shrivastava, who is a Microsoft MVP in the Business Applications category. I am a Microsoft Dynamics 365 and Power Platform enthusiast person who is having a passion for researching and learning new things and acquiring immense knowledge. I am providing consistent help, support, and sharing my knowledge through various Social Media Channels along with my Personal Blog, Microsoft Community, conducting online training and attending various 365 Saturday Events worldwide and sharing the best Solutions to the readers helping them achieve their goals and objectives in Customer Relationship Space.

One thought on “Convert QueryExpression to FetchXML and FetchXML to QueryExpression in MSCRM

  1. Special thanks to ( for exposing my cheating husband. Right with me i got a lot of evidences and proofs that shows that my husband is a fuck boy and as well a cheater ranging from his text messages, call logs, whats-app messages, deleted messages and many more, All thanks to (, if not for him i will never know what has been going on for a long time. Contact him now and thank me later.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: