2

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!

yofi
  • 33
  • 7

2 Answers2

3

You should be able to build your query in Advanced find & Download the fetchxml from there to use in JavaScript. Read more

You can use XrmToolBox - FetchXml Builder as well for building queries (this will give SQL, QueryExpression equivalent too), then generate language compatible output using this online formatter tool

Code sample to use fetchxml in JavaScript can be find in this blog. For Example:

var encodedFetchXml = encodeURI(fetchContact);
var reqURL = clientURL + “/api/data/v8.2/contacts?fetchXml=” + encodedFetchXml;
var req = new XMLHttpRequest();
req.open(“GET”, reqURL, false);

QueryExpression, FetchXml, LINQ are all choices to write the same query. As you know - QueryExpression cannot be used in JavaScript, but fetchxml can be used in both C# & JS.

  • Thank you for your reply. I am mainly inserted in dynamically creating the FetchXml content. The 'ColumnSet ', 'FilterExpression', and 'ConditionExpression' are all going to be dynamic. they change based on the number of fields, type of filter/filters passed. Obviously, my code doesn't show that in its current state. – yofi Jan 25 '19 at 14:01
  • 1
    One question, is there a JS equivalent for this code: `QueryExpressionToFetchXmlResponse resp = (QueryExpressionToFetchXmlResponse)service.Execute(req);` ? – yofi Jan 25 '19 at 14:11
  • 1
    QueryExpressionToFetchXml exists only in the .NET SDK. The WebAPI lacks any QueryExpression functionality. – Aron Jan 25 '19 at 14:47
2

In addition to @Arun's excellent answer, another possibility would be to keep your logic in C# and register it in the system as a custom Action, which you then call from JavaScript.

Aron
  • 3,877
  • 3
  • 14
  • 21
  • 1
    Unfortunately, I will be using this logic in an ESB called Mule. The only other alternative I have is Java. They have a connector for .Net, but I am trying to avoid that.. – yofi Jan 25 '19 at 14:04
  • Understood. In that case, working with the WebAPI either directly via OData or FetchXML is the way to go. – Aron Jan 25 '19 at 14:44