如何使用CrmServiceClient设置null属性值

时间:2018-05-22 23:25:37

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

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

我的代码适用于非null值,但失败并出现异常:

  

对象引用未设置为对象的实例。在   Microsoft.Xrm.Tooling.Connector.CrmServiceClient.AddValueToPropertyList(KeyValuePair 2 Field, AttributeCollection PropertyList) at Microsoft.Xrm.Tooling.Connector.CrmServiceClient.UpdateEntity(String entityName, String keyFieldName, Guid id, Dictionary 2 fieldList,   String applyToSolution,Boolean enabledDuplicateDetection,Guid   batchId)

每当我尝试设置空值时。

到目前为止,我已尝试过以下内容:

// 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年才可用。

1 个答案:

答案 0 :(得分:1)

这应该有用。

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