You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Dynamics CRM 2016服务器端插件克隆记录技术咨询

关于Dynamics CRM 2016记录克隆插件的问题解答

1. 从CRM 2015 SDK提取Microsoft.Xrm.Client库用于2016环境是否受官方支持?

很明确地说,这种做法是不被微软官方支持的。Microsoft.Xrm.Client库在2016 SDK中被移除是架构调整的结果,后续版本的SDK和平台已经不再依赖这个库来实现克隆功能。跨版本混用SDK库可能会引发一系列问题:

  • 兼容性风险:2015库的内部实现可能和2016平台的API存在不匹配,导致插件运行时抛出异常(比如类型转换错误、方法签名不兼容)
  • 后续升级隐患:如果未来要升级到更高版本的CRM,这个混用的库会成为阻碍,而且微软不会为这类非官方配置提供技术支持
  • 稳定性问题:非官方支持的组件可能在平台更新后突然失效,影响业务流程

2. 不使用该库时,服务器端克隆记录的可行方案

如果不能用那个库,有几个可靠的服务器端克隆方案,都是官方支持的:

方案一:手动实现实体克隆逻辑

这是最灵活也最可控的方式,步骤大概是:

  • 检索源实体的所有字段(注意排除系统专属字段,比如createdon、createdby、ownerid、statecode、statuscode这些,还有主键id)
  • 创建目标实体的实例,将源实体的非系统字段值逐一复制过去
  • 调用Create方法保存新实体

示例代码片段:

// 假设要克隆Account实体
var sourceAccount = service.Retrieve("account", sourceId, new ColumnSet(true)); // 检索所有字段
var targetAccount = new Entity("account");

// 遍历源实体属性,排除系统字段
foreach (var attribute in sourceAccount.Attributes)
{
    // 跳过系统字段
    if (attribute.Key.Equals("accountid", StringComparison.OrdinalIgnoreCase) ||
        attribute.Key.Equals("createdon", StringComparison.OrdinalIgnoreCase) ||
        attribute.Key.Equals("createdby", StringComparison.OrdinalIgnoreCase) ||
        attribute.Key.Equals("ownerid", StringComparison.OrdinalIgnoreCase) ||
        attribute.Key.Equals("statecode", StringComparison.OrdinalIgnoreCase) ||
        attribute.Key.Equals("statuscode", StringComparison.OrdinalIgnoreCase))
    {
        continue;
    }
    targetAccount[attribute.Key] = attribute.Value;
}

// 创建新记录
var newAccountId = service.Create(targetAccount);

方案二:使用OrganizationService的DeepCloneRequest(适用于支持的实体)

从CRM 2016开始,部分实体支持DeepCloneRequest消息,这个是官方提供的深度克隆功能,可以复制实体以及关联的相关记录(比如联系人、注释等,取决于实体配置)。

示例代码:

var cloneRequest = new DeepCloneRequest
{
    Target = new EntityReference("account", sourceId),
    // 可选:指定要克隆的关联实体,如果不指定则克隆所有支持的关联
    RelatedEntitiesToClone = new List<string> { "contact_customer_accounts", "annotation" }
};

var cloneResponse = (DeepCloneResponse)service.Execute(cloneRequest);
var clonedEntityId = cloneResponse.ClonedEntity.Id;

注意:不是所有实体都支持DeepCloneRequest,可以先查官方文档确认目标实体是否支持这个消息。

方案三:使用FetchXML检索并映射字段

如果需要更精细地控制要克隆的字段,可以用FetchXML来指定要获取的字段,然后映射到新实体,这样可以避免检索不必要的字段,提升性能。

示例代码:

var fetchXml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                    <entity name='account'>
                        <attribute name='name' />
                        <attribute name='address1_line1' />
                        <attribute name='address1_city' />
                        <!-- 添加需要克隆的其他字段 -->
                        <filter type='and'>
                            <condition attribute='accountid' operator='eq' value='{0}' />
                        </filter>
                    </entity>
                </fetchXml>";

fetchXml = string.Format(fetchXml, sourceId.ToString());
var fetchResult = service.RetrieveMultiple(new FetchExpression(fetchXml));

if (fetchResult.Entities.Count > 0)
{
    var sourceAccount = fetchResult.Entities[0];
    var targetAccount = new Entity("account");
    foreach (var attribute in sourceAccount.Attributes)
    {
        targetAccount[attribute.Key] = attribute.Value;
    }
    var newId = service.Create(targetAccount);
}

这些方案都是基于官方SDK的API实现,完全受微软支持,而且在后续版本的CRM中也能保持兼容性。

内容的提问来源于stack exchange,提问作者Damkulul

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:19:44