I am trying to get the FetchXML query for a specific query expression fro Dynamics CRM. I have manged to do it using the XRM SDK in a C# project as follows.
string connectionStr = @"Server=https://mycompany.com/XRMServices/2011/Organization.svc; Username=theUserName; Password=pwd";
Microsoft.Xrm.Client.CrmConnection conn = Microsoft.Xrm.Client.CrmConnection.Parse(connectionStr);
var service = new Microsoft.Xrm.Client.CrmOrganizationServiceContext(conn);
var query = new QueryExpression();
QueryExpressionToFetchXmlRequest req = new QueryExpressionToFetchXmlRequest();
query.EntityName = "my_entity";
query.ColumnSet = new ColumnSet("_accountnumber", "_id");
FilterExpression filter = new FilterExpression();
filter.Conditions.Add(new ConditionExpression("_Oactivedate", ConditionOperator.NotNull));
filter.Conditions.Add(new ConditionExpression("_Oinactivedate", ConditionOperator.Null));
filter.Conditions.Add(new ConditionExpression("_state", ConditionOperator.Equal, 0));
FilterExpression filter1 = new FilterExpression(LogicalOperator.Or);
var filter2 = new FilterExpression(LogicalOperator.And);
filter2.Conditions.Add(new ConditionExpression("_lastname", ConditionOperator.Equal, lastName));
filter2.Conditions.Add(new ConditionExpression("_accountnumber", ConditionOperator.Equal, accountId));
var filter3 = new FilterExpression(LogicalOperator.And);
filter3.Conditions.Add(new ConditionExpression("_accountactivedate", ConditionOperator.NotNull));
filter3.Conditions.Add(new ConditionExpression("_accountinactivedate", ConditionOperator.Null));
filter1.AddFilter(filter2);
filter1.AddFilter(filter3);
filter.AddFilter(filter1);
query.Criteria = filter;
req.Query = query;
QueryExpressionToFetchXmlResponse resp = (QueryExpressionToFetchXmlResponse)service.Execute(req);
//fetchxml string
string myfetch = resp.FetchXml;
My requirement doesn't allow for a .Net DLL, therefore I would like to do this in JS. I have tried to look at the JS SDK and it is not as friendly as the C# one (maybe it's just me :). I would really appreciate anyone who can attempt to write a JS equivalent of my code above, or guide to a good material that can help me figure it out myself. My main interest is the generation of the FetchXml query from a dynamic strongly typed QueryExpression object. Thanks!