CRM 2011 - 拦截快速搜索,获取xml和查询表达式不会返回相同的内容吗?

时间:2011-12-07 00:02:58

标签: c# dynamics-crm-2011

我正在为某些实体的快速搜索框创建搜索快捷方式。这是为了避免多次返回,尤其是当名称可以包含城市名称时。 (城市搜索是相关的,所以必须留下来)

我正在通过插件完成此操作。所以用户输入

/name Todd Richardson

在联系人实体视图的搜索框中。

更新

拦截(预操作阶段:20 预验证阶段:10)对联系人的Retrievemultiple请求。

结束更新

更新这里要求的是从MSCRM 2011 sdk工具生成然后修改的实现的开始请记住此代码处于原型状态,可能不适合生产代码:

protected void ExecutePreAccountRetrieveMultiple(LocalPluginContext localContext)
    {
        if (localContext == null)
        {
            throw new ArgumentNullException("localContext");
        }

        if (localContext.PluginExecutionContext.InputParameters.Contains("Query"))
        {
            if (localContext.PluginExecutionContext.InputParameters["Query"] is QueryExpression)
            {
                //query expression from input is assigned to a local variable for modification.
                QueryExpression qe = (QueryExpression)localContext.PluginExecutionContext.InputParameters["Query"];

                if (qe.Criteria != null)
                {
                    if (qe.Criteria.Filters.Count > 1)
                    {
                        string entitySubject = qe.EntityName;
                        string searchSubject = qe.Criteria.Filters[1].Conditions[0].Values[0].ToString();


                         string namePattern = @"^([/\\-])+N(AME)?:?[\s]*(.+$)";

 //.... Eliminated for brevity, only including branch thats relevant to this question.


if (Regex.IsMatch(searchSubject.TrimEnd("%".ToCharArray()), namePattern, RegexOptions.IgnoreCase))
                            {
                                var Match = Regex.Match(searchSubject.TrimEnd("%".ToCharArray()), namePattern, RegexOptions.IgnoreCase);


                                if (Match.Groups.Count > 1)
                                {
                                    int lastIndex = Match.Groups.Count - 1;
                                    string name = Match.Groups.Cast<Group>().Last().Value;
                                    Func<string, List<ConditionExpression>> genXpress = (n) =>
                                    {

                                        List<ConditionExpression> ce = new List<ConditionExpression>();

                                        foreach (var val in name.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(x => string.Format("%{0}%", x)))
                                        {
                                            ce.Add(new ConditionExpression
                                            {
                                                Operator = ConditionOperator.Like,
                                                AttributeName = n,
                                                Values = { val }
                                            });
                                        }
                                        return ce;
                                    };

                                    if (entitySubject == "contact")
                                    {

                                        string[] names = name.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                                        if (names.Length > 1)
                                        {
                                            string fn = names[names.Length - 2];
                                            string ln = names[names.Length - 1];

                                            string fetchRequest =
@"<?xml version=""1.0""?>
<fetch distinct=""false"" mapping=""logical"" output-format=""xml-platform"" version=""1.0""> 
    <entity name=""contact""> <attribute name=""fullname""/> 
    <attribute name=""telephone1""/> <attribute name=""contactid""/> 
    <order descending=""false"" attribute=""fullname""/> 
        <filter type=""and""> 
            <filter type=""or""> 
                <filter type=""and""> 
                    <condition attribute=""lastname"" value=""%%lastname%%"" operator=""like""/> 
                    <condition attribute=""firstname"" value=""%%firstname%%"" operator=""like""/> 
                </filter>  
                <filter type=""and""> 
                    <condition attribute=""lastname"" value=""%%firstname%%"" operator=""like""/> 
                    <condition attribute=""firstname"" value=""%%lastname%%"" operator=""like""/> 
                </filter>
            </filter> 
        </filter> 
    </entity> 
</fetch>" //

                                            .Replace("%lastname%", ln).Replace("%firstname%", fn);


                                            var conversionRequest = new FetchXmlToQueryExpressionRequest
                                            {
                                                FetchXml = fetchRequest
                                            };
                                            var response = (FetchXmlToQueryExpressionResponse)localContext.OrganizationService.Execute(conversionRequest);

                                            localContext.PluginExecutionContext.OutputParameters["Query"] = response.Query;
                                            return;
                                        }
                                        //variable modified and now passed out for execution.
                                        localContext.PluginExecutionContext.OutputParameters["Query"] = qe;


                                        return;
                                    }
                                }
                            }  //Remainder of code eliminated for different logic branches.

结束更新

生成查询表达式并将其放入名为query的输出参数中。

最初我正在构建QueryExpression。我发现这不起作用。无论我如何构建我的查询表达式,我都得到了

condition1 || condition 2 || condition3 || condition4 

所以我采取了另一个角度。我转到高级查找并创建了一个查询,它在结果中返回了我想要的内容。我下载了fetch-xml,现在这就是我所拥有的(如前面的代码所示):

string fetchRequest =
@"<?xml version=""1.0""?>
<fetch distinct=""false"" mapping=""logical"" output-format=""xml-platform"" version=""1.0""> 
    <entity name=""contact""> <attribute name=""fullname""/> 
    <attribute name=""telephone1""/> <attribute name=""contactid""/> 
    <order descending=""false"" attribute=""fullname""/> 
        <filter type=""and""> 
            <filter type=""or""> 
                <filter type=""and""> 
                    <condition attribute=""lastname"" value=""%%lastname%%"" operator=""like""/> 
                    <condition attribute=""firstname"" value=""%%firstname%%"" operator=""like""/> 
                </filter>  
                <filter type=""and""> 
                    <condition attribute=""lastname"" value=""%%firstname%%"" operator=""like""/> 
                    <condition attribute=""firstname"" value=""%%lastname%%"" operator=""like""/> 
                </filter>
            </filter> 
        </filter> 
    </entity> 
</fetch>" //

无论是在代码中生成Queryexpression,还是从组织服务中获取它,它似乎都得到了相同的结果。而不是

(condition1 && condition2) || (condition3 && condition4) 

满足标准,它基本上结束了

condition1 || condition2 || condition3 || condition4 

我在fetch xml上尝试了其他变体,包括:

string fetchRequest =  @"<?xml version=""1.0""?>
<fetch distinct=""false"" mapping=""logical"" output-format=""xml-platform"" version=""1.0""> 
    <entity name=""contact""> <attribute name=""fullname""/> 
    <attribute name=""telephone1""/> <attribute name=""contactid""/> 
    <order descending=""false"" attribute=""fullname""/> 
        <filter type=""and""> 
                <condition attribute=""lastname"" value=""%%lastname%%"" operator=""like""/> 
                <condition attribute=""firstname"" value=""%%firstname%%"" operator=""like""/> 
        </filter> 
    </entity> 
</fetch>"

再次,这最终成为

condition1 || condition2

condition1 && condition2

任何人都知道发生了什么。我应该使用不同的fetchxml吗?这是一个错误吗?答案一直在让我度过一天的美好时光。

希望这是一件容易让我俯瞰的事情。

2 个答案:

答案 0 :(得分:3)

您正在将修改后的查询添加到OutputParameters集合中。这不正确; CRM事件管道不期望Query参数在此集合中,因此忽略它。这就是您没有看到所需结果集的原因。

您必须将InputParameters集合中的现有Query参数替换为您修改的参数。

答案 1 :(得分:2)

在您的代码中,您尝试在Pre阶段插件时更新OutputParameters。根据文档,它可能没有任何影响,可能会在平台核心操作阶段被覆盖。 IPluginExecutionContext.OutputParameters Property