Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# CRM 2011-拦截快速搜索、获取xml和查询表达式don';你不能退回同样的东西吗?_C#_Dynamics Crm 2011 - Fatal编程技术网

C# CRM 2011-拦截快速搜索、获取xml和查询表达式don';你不能退回同样的东西吗?

C# CRM 2011-拦截快速搜索、获取xml和查询表达式don';你不能退回同样的东西吗?,c#,dynamics-crm-2011,C#,Dynamics Crm 2011,我正在从“快速搜索”框中为某些实体创建搜索快捷方式。这是为了避免多次返回,尤其是当名称可能包含城市名称时。(城市搜索是相关的,因此必须保留) 我通过一个插件来实现这一点。因此,用户进入 /name Todd Richardson 在“联系人实体”视图的“搜索”框中 更新 这将拦截(操作前阶段:20预验证阶段:10)对联系人的Retrievemultiple请求 结束更新 按要求更新此处是从MSCRM 2011 sdk工具生成并修改的实施的开始。请记住,此代码处于原型状态,可能不适用于生产代码:

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

我通过一个插件来实现这一点。因此,用户进入

/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.
所以我换了一个角度。我转到高级查找并创建了一个查询,该查询返回的结果正好是我想要的结果。我下载了FetchXML,现在是我所拥有的(如前面的代码所示):

达到标准后,基本上就结束了

condition1 || condition2 || condition3 || condition4 
我已经尝试了提取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>"
不是

任何人都知道发生了什么事。我是否应该使用不同的fetchxml?这是虫子吗?今天的大部分时间里,答案一直在困扰着我


希望这只是我忽略的一些简单的事情

在您的代码中,您正试图在前期插件中更新OutputParameters。根据文件,它可能没有效果,并且可能在平台核心操作阶段被覆盖

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


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

请显示插件的一部分,您可以在其中截取和修改查询吗?谢谢您的回答。我会调查这件事,然后退房+1.等了这么久才回复!我不认为这是我正在经历的问题。我始终更改查询表达式的参数,并且它们不会被覆盖。举个例子:我有一个查询,在前缀前面加上一个特定的前缀时,会根据前缀后面输入的数字获取一个字段。每次都有效。有关此特定查询的某些内容无法正确处理。我担心我们现在会暂时搁置这个功能,但在将来的某个时候,我会重新考虑。当我有时间的时候,我会检验这个假设。然而,我对这一次并没有太高的期望。如果您不将参数放入输出集合中,它似乎不会修改查询,我将继续并接受这个答案。我最后在这里的一个dynamics博客上对此进行了研究:发现您确实应该修改输入参数的“查询”属性。我无法测试这一点,因为我不再参加这场演出了,但最终结束这个问题是件好事。如果我得到一个CRM实例来测试它,我将重新讨论这个问题。
(condition1 && condition2) || (condition3 && condition4) 
condition1 || condition2 || condition3 || condition4 
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