MS CRM 4中,添加营销列表成员查找列

    MS CRM4中,市场营销列表成员(其架构名称是listmember)是一个系统内部所使用的实体,没有被开放出来,在实体列表中是看不到该实体的。正好群里面有一个帖子,询问如何在营销列表中添加对联系人的“公司电话”的搜索。默认情况下,是对联系人名称或者客户名称进行搜索。

    下图是所有的市场营销列表成员

image

    在搜索框中输入“张三”,可以得到名字为“张三”的联系人记录,如下如所示

image

    而输入“123”这个查找条件,代表着我希望“公司电话”也作为查找列,可惜,结果不如所愿——没有匹配的记录结果,如下图所示。

image

    由此可见,系统只将“全名”字段作为查找列,需求来了,需要将联系人的“公司电话”也作为查找列,怎么办?我的解决方式就是Plugin,对“Execute”消息进行捕获,在plugin中编写代码进行处理。

    创建一个plugin,注册时,将消息设定为Execute,然后debug代码,得到IPluginExecutionContext类型的输入参数的值如下图所示。

image

    从上图可见,InputParameters有一个Key名称为“FetchXml”的元素值,其对应的Value是一个fetchxml字符串,内容如下:

<fetch distinct="false" mapping="logical">
    <entity name="contact">
        <attribute name="fullname" />
        <attribute name="address1_city" />
        <attribute name="telephone1" />
        <attribute name="contactid" />
        <filter type="and">
            <filter type="or">
                <condition attribute="fullname" operator="like" value="123%" />
            </filter>
        </filter>
        <order attribute="fullname" descending="false" />
        <link-entity name="listmember" to="contactid" from="entityid" link-type="inner" alias="listmember0">
            <filter type="and">
                <condition attribute="listid" operator="eq" value="{FDA0C303-D308-E011-90D0-00155D000107}" />
            </filter>
        </link-entity>
    </entity>
</fetch>

    OK,看到这个字符串,很明显,可以看到查询条件就是标记为红色那一行,大胆猜测一下,我添加一个条件,设定其attribute的值为“telephone1”,那么就可以将“公司电话”也作为查找列了。

    开始编码……,还是稍等一下吧。因为Execute方法并非就当前这一个地方使用了,例如,利用联系人Grid上的搜索框进行查找时,系统也是用Execute方法。所以,还需要在代码中使用各类条件进行判断以缩小范围,做到只有在营销列表成员中查找联系人时,才将联系人的“公司电话”列作为查找列,那么如何做到这一点呢,解决方法就在“link-entity"节点中。

    前文说了,listmember是一个内部使用的实体,所以,将link-entity节点下的属性”name”、“to”作为判断条件,以限定、缩小范围,从而满足只有在营销列表成员中查找联系人时,才将联系人的“公司电话”列作为查找列这一目标。

    基于以上,写出了代码

if (context.MessageName == "Execute")
{
    string fetchXml = (string)context.InputParameters["FetchXml"];
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(fetchXml);
    XmlNodeList linkEntityNodeList = doc.GetElementsByTagName("link-entity");
    if (linkEntityNodeList !=null && linkEntityNodeList.Count > 0)
    {
        string nameAttrValue = linkEntityNodeList[0].Attributes["name"].Value;
        string toAttrValue = linkEntityNodeList[0].Attributes["to"].Value;
        if (nameAttrValue == "listmember" && toAttrValue == "contactid")
        {
            XmlNode filterNode = doc.SelectSingleNode("/fetch/entity/filter/filter");

            if (filterNode != null)
            {
                XmlNode existConditionNode = filterNode.FirstChild;

                XmlElement conditionNode = doc.CreateElement("condition");
                conditionNode.Attributes.Append(doc.CreateAttribute("attribute")).Value = "telephone1";
                conditionNode.Attributes.Append(doc.CreateAttribute("operator")).Value = "like";
                conditionNode.Attributes.Append(doc.CreateAttribute("value")).Value = existConditionNode == null ? "" : existConditionNode.Attributes["value"].Value;

                filterNode.AppendChild(conditionNode);
            }
        }
    }

    context.InputParameters["FetchXml"] = doc.OuterXml;
}

 

 

 

    编译、部署,测试,结果如下图所示

    输入电话号码,可以得到张三这条记录

image

    输入“张三”,也可以得到张三这条记录

image

 

    当然,测试没有进行彻底,不清楚是否对其他的部分造成什么影响。只能够算是指出来一个想法吧。

转载于:https://www.cnblogs.com/StoneGarden/archive/2011/01/13/1934891.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值