On PREM Dynamics CRM 2016 JavaScript equivalent of my C# QueryExpression

好久不见. 提交于 2020-01-15 08:14:31

问题


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!


回答1:


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.




回答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.



来源:https://stackoverflow.com/questions/54356648/on-prem-dynamics-crm-2016-javascript-equivalent-of-my-c-sharp-queryexpression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!