在PREM Dynamics CRM 2016 JavaScript上相当于我的C#QueryExpression

时间:2019-01-24 23:02:09

标签: javascript c# dynamics-crm dynamics-crm-2016 query-expressions

我正在尝试通过Dynamics CRM获取针对特定查询表达式的FetchXML查询。我已经设法在C#项目中使用XRM SDK来做到这一点,

       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; 

我的要求不允许使用.Net DLL,因此我想在JS中这样做。我尝试查看JS SDK,它不如C#友好(也许只是我自己:)。我真的很感激任何可以尝试编写与我上面的代码等效的JS的人,或者提供了可以帮助我自己理解的优秀材料的指南。我的主要兴趣是从动态强类型QueryExpression对象生成FetchXml查询。谢谢!

2 个答案:

答案 0 :(得分:3)

您应该能够在“高级查找”中构建查询,并从此处下载fetchxml以在JavaScript中使用。 Read more

您也可以使用XrmToolBox - FetchXml Builder for building queries(这也将提供SQL和QueryExpression等效项),然后使用此online formatter tool

生成与语言兼容的输出

可以在this blog中找到在JavaScript中使用fetchxml的代码示例。例如:

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都是编写同一查询的选择。如您所知-QueryExpression不能在JavaScript中使用,但是fetchxml可以在C#和JS中使用。

答案 1 :(得分:2)

除了@Arun的出色答案外,另一种可能性是将您的逻辑保留在C#中,并在系统中将其注册为custom Action,然后由您call from JavaScript注册。