【问题标题】:How to use CrmServiceClient to set a null attribute value如何使用 CrmServiceClient 设置空属性值
【发布时间】:2018-05-22 23:25:36
【问题描述】:

我正在努力使用 CrmServiceClient 将属性值更新为 null。

我的代码对非空值运行良好,但因异常而失败:

对象引用未设置为对象的实例。在 Microsoft.Xrm.Tooling.Connector.CrmServiceClient.AddValueToPropertyList(KeyValuePair2 Field, AttributeCollection PropertyList) at Microsoft.Xrm.Tooling.Connector.CrmServiceClient.UpdateEntity(String entityName, String keyFieldName, Guid id, Dictionary2 fieldList, 字符串 applyToSolution、Boolean enabledDuplicateDetection、Guid 批号)

每当我尝试设置空值时。

到目前为止,我已经尝试了以下方法:

// create Crm service client object
CrmServiceClient svc = new CrmServiceClient(
new System.Net.NetworkCredential("username", "password", "domain"),
                "crmhost",
                "443",
                "crmorganization",
                false,
                true
                );

// check the connection to CRM was successful
if (!svc.IsReady)
{
    throw new Exception("Could not connect to CRM Organization.", svc.LastCrmException);
}

// create a Dictionary to store the attributes to be added to the entity
Dictionary<string, CrmDataTypeWrapper> attributes = new Dictionary<string, CrmDataTypeWrapper>();

//this doesn't work
attributes.Add("new_somedatefield", null);

// and this doesn't work
attributes.Add("new_somedatefield", new CrmDataTypeWrapper(null, CrmFieldType.CrmDateTime));

// this also doesn't work
DateTime? nullDate = new DateTime();
nullDate = null;
attributes.Add("new_somedatefield", new CrmDataTypeWrapper(nullDate, CrmFieldType.CrmDateTime));

svc.UpdateEntity("new_entityname", "new_entitynameid", (Guid)data[metaData.PrimaryIdAttribute], attributes));

文档中没有特别提到设置空值。

有没有人成功做到这一点?

编辑 - 为了澄清我需要以 Dynamics CRM 2015 为目标,CrmServiceClient 上的 Update 方法直到 2016 年才可用。

【问题讨论】:

  • 为什么不能使用最新的 Xrm.Tooling?它可以成功调用CRM 2015,没有理由在插件之外使用过时的库
  • 是的,这似乎奏效了。使用@Arun 的建议,我现在可以成功设置空值。我假设我需要使用与我们的 CRM 版本匹配的 Xrm.Tooling。在将其投入生产之前,我将运行更多测试和场景。仍然在问我应该如何使用我使用的版本来实现它!

标签: c# dynamics-crm microsoft-dynamics dynamics-crm-2015 xrm


【解决方案1】:

这应该可行。

Entity ent = new Entity("entityname");
ent.Attributes["datefieldname"] = null;
ent.id = Id;
service.Update(ent);

【讨论】:

  • 这不是 CrmServiceClient 的使用方式。没有接受 Entity 对象的 Update 方法。您需要构建一个 字典作为属性列表,字符串是 CRM 属性的名称,CrmDataTypeWrapper 对象包含要设置属性的值。我无法弄清楚如何使用 CrmDataTypeWrapper 对象设置 NULL 值而不产生上述异常。
  • 看起来更新方法只适用于从 CRM 2016 起,我们使用的是 CRM 2015。我已经更新了问题以澄清。
  • 采纳@Pawel 的建议,我已更新为使用最新版本的 Xrm.Tooling,这意味着我可以使用 Arun 的建议。仍然不确定如何使用我最初使用的旧版本来实现它。
猜你喜欢
  • 2011-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-16
  • 1970-01-01
  • 2021-03-06
  • 2012-01-22
  • 2019-11-21
相关资源
最近更新 更多