dynamics-crm - Dynamics CRM 4.0 中的自定义搜索

标签 dynamics-crm dynamics-crm-4

我有两个相关的问题。

第一的:
我希望对 Dynamics CRM 4.0 中的自定义实体进行全文搜索。有没有人以前做过这个或知道怎么做?

我知道我可以使用 Web 服务和 sdk 构建 QueryExpressions,但是我可以使用这种方法使用 bool 类型语法进行全文搜索吗?据我所知,这不会奏效。

第二:
有没有其他人觉得 Dynamics CRM 4.0 提供的搜索功能受到限制?我知道有一些第三方搜索产品,但我还没有找到我喜欢的产品。任何建议,将不胜感激。

最佳答案

通过 CRM SDK 进行搜索和过滤确实需要一些时间来适应。为了模拟全文搜索,您需要使用嵌套的 FilterExpressions 作为您的 QueryExpression.Criteria。 SDK page for nested filters最难的部分是弄清楚如何建立父子关系。有太多 bool 逻辑在进行,很容易迷路。

我需要为我们的自定义实体之一构建“搜索引擎”。将此方法用于具有多个可搜索属性的复杂搜索字符串(“一和二或三”)是很丑陋的。不过如果你有兴趣,我可以挖掘它。虽然它并不真正受支持,但如果您可以直接访问数据库,我建议使用 SQL 的全文搜索功能。

——
好的,给你。我认为您无法复制粘贴并满足您的需求。我的客户只进行了两到三个关键词搜索,他们对搜索结果感到满意。您可以看到在简单的搜索场景中执行此操作是多么痛苦。我基本上把代码吐出来,直到它“工作”。

    private FilterExpression BuildFilterV2(string[] words, string[] seachAttributes)
    {
        FilterExpression filter = new FilterExpression();
        List<FilterExpression> allchildfilters = new List<FilterExpression>();

        List<string> andbucket = new List<string>();
        List<string> orBucket = new List<string>();

        // clean up commas, quotes, etc
        words = ScrubWords(words);

        int index = 0;

        while (index < words.Length)
        {
            // if current word is 'and' then add the next wrod to the ad bucket
            if (words[index].ToLower() == "and")
            {
                andbucket.Add(words[index + 1]);
                index += 2;
            }
            else
            {
                if (andbucket.Count > 0)
                {

                    List<FilterExpression> filters = new List<FilterExpression>();
                    foreach (string s in andbucket)
                    {
                        filters.Add(BuildSingleWordFilter(s, seachAttributes));
                    }

                    // send existing and bucket to condition builder 
                    FilterExpression childFilter = new FilterExpression();
                    childFilter.FilterOperator = LogicalOperator.And;
                    childFilter.Filters = filters.ToArray();

                    // add to child filter list
                    allchildfilters.Add(childFilter);

                    //new 'and' bucket
                    andbucket = new List<string>();
                }
                if (index + 1 < words.Length && words[index + 1].ToLower() == "and")
                {
                    andbucket.Add(words[index]);
                    if (index + 2 <= words.Length)
                    {
                        andbucket.Add(words[index + 2]);
                    }
                    index += 3;
                }
                else
                {
                    orBucket.Add(words[index]);
                    index++;
                }
            }
        }

        if (andbucket.Count > 0)
        {
            List<FilterExpression> filters = new List<FilterExpression>();
            foreach (string s in andbucket)
            {
                filters.Add(BuildSingleWordFilter(s, seachAttributes));
            }

            // send existing and bucket to condition builder 
            FilterExpression childFilter = new FilterExpression();
            childFilter.FilterOperator = LogicalOperator.And;
            childFilter.Filters = filters.ToArray();

            // add to child filter list
            allchildfilters.Add(childFilter);

            //new 'and' bucket
            andbucket = new List<string>();
        }
        if (orBucket.Count > 0)
        {
            filter.Conditions = BuildConditions(orBucket.ToArray(), seachAttributes);
        }
        filter.FilterOperator = LogicalOperator.Or;
        filter.Filters = allchildfilters.ToArray();

        return filter;
    }
    private FilterExpression BuildSingleWordFilter(string word, string[] seachAttributes)
    {
        List<ConditionExpression> conditions = new List<ConditionExpression>();

        foreach (string attr in seachAttributes)
        {
                ConditionExpression expr = new ConditionExpression();
                expr.AttributeName = attr;
                expr.Operator = ConditionOperator.Like;
                expr.Values = new string[] { "%" + word + "%" };

                conditions.Add(expr);
        }

        FilterExpression filter = new FilterExpression();
        filter.FilterOperator = LogicalOperator.Or;
        filter.Conditions = conditions.ToArray();

        return filter;
    }

    private ConditionExpression[] BuildConditions(string[] words, string[] seachAttributes)
    {
        List<ConditionExpression> conditions = new List<ConditionExpression>();

        foreach (string s in words)
        {
            foreach (string attr in seachAttributes)
            {
                ConditionExpression expr = new ConditionExpression();
                expr.AttributeName = attr;
                expr.Operator = ConditionOperator.Like;
                expr.Values = new string[] { "%" + s + "%" };

                conditions.Add(expr);
            }
        }

        return conditions.ToArray();
    }

关于dynamics-crm - Dynamics CRM 4.0 中的自定义搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/450462/

相关文章:

dynamics-crm - MS Dynamics CRM 在线计算字段错误

c# - 使用自定义按钮从 CRM 中执行存储过程的最佳方法

c# - Dynamics CRM - 在新创建实体的工作流中访问属性

javascript - 使用 OData 检索 Dynamics CRM 中的多个实体

javascript - 如何编辑首选联系方法字段中显示的选择字段

javascript - SuccessCallback 在 Xrm.Page.data.save 中立即触发

.net - 在 Microsoft CRM 4.0 中使用 Web 服务与使用动态实体

c# - 多个执行调用

c# - 从 CRM 4 和 2011 中的站点地图打开 ASPX 页面

javascript - 使用 Javascript 访问 MS CRM 4.0 中 IFrame 中加载的页面上的控件