Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用CrmServiceClient设置空属性值_C#_Dynamics Crm_Microsoft Dynamics_Dynamics Crm 2015_Xrm - Fatal编程技术网

C# 如何使用CrmServiceClient设置空属性值

C# 如何使用CrmServiceClient设置空属性值,c#,dynamics-crm,microsoft-dynamics,dynamics-crm-2015,xrm,C#,Dynamics Crm,Microsoft Dynamics,Dynamics Crm 2015,Xrm,我正在努力使用CrmServiceClient将属性值更新为null 对于非空值,我的代码工作正常,但由于异常而失败: 对象引用未设置为对象的实例。在 Microsoft.Xrm.Tooling.Connector.CrmServiceClient.AddValueTopPropertyList(KeyValuePair2 字段,AttributeCollection PropertyList)位于 Microsoft.Xrm.Tooling.Connector.CrmServiceClient

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

对于非空值,我的代码工作正常,但由于异常而失败:

对象引用未设置为对象的实例。在 Microsoft.Xrm.Tooling.Connector.CrmServiceClient.AddValueTopPropertyList(KeyValuePair
2
字段,AttributeCollection PropertyList)位于
Microsoft.Xrm.Tooling.Connector.CrmServiceClient.UpdateEntity(字符串
entityName、字符串keyFieldName、Guid id、字典
2字段列表、, 字符串applyToSolution,布尔启用重复检测,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));
//创建Crm服务客户端对象
CrmServiceClient svc=新的CrmServiceClient(
新的System.Net.NetworkCredential(“用户名”、“密码”、“域”),
“crmhost”,
"443",
“组织机构”,
假,,
真的
);
//检查与CRM的连接是否成功
如果(!svc.IsReady)
{
抛出新异常(“无法连接到CRM组织。”,svc.lastCrmeException);
}
//创建字典以存储要添加到实体的属性
字典属性=新字典();
//这不管用
Add(“new_somedatefield”,null);
//这不管用
Add(“new_somedatefield”,new CrmDataTypeWrapper(null,CrmFieldType.CrmDateTime));
//这也不起作用
约会时间?nullDate=新的日期时间();
nullDate=null;
Add(“new_somedatefield”,new CrmDataTypeWrapper(nullDate,CrmFieldType.CrmDateTime));
UpdateEntity(“new_entityname”、“new_entitynameid”、(Guid)数据[metaData.primarydataAttribute],属性]);
文档中没有特别提到设置空值

有人成功地做到了这一点吗

编辑-为了澄清我需要瞄准Dynamics CRM 2015,CrmServiceClient上的更新方法要到2016年才可用。

这应该有效

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

这不是CrmServiceClient的使用方式。没有接受实体对象的更新方法。您需要构建一个字典作为属性列表,字符串是CRM属性的名称,CrmDataTypeWrapper对象包含要设置属性的值。我无法解决如何使用CrmDataTypeWrapper对象设置空值,而不生成如上所述的异常。vs看起来更新方法仅适用于2016年以后的CRM,我们使用的是2015年CRM。我已经更新了问题以澄清。根据@Pawel的建议,我已经更新使用最新版本的Xrm.Tooling,这意味着我可以使用Arun的建议。仍然不确定如何使用我最初使用的旧版本实现它。为什么不能使用最新的Xrm.Tooling?它可以成功调用CRM 2015,没有理由在插件之外使用过时的LIB是的,这似乎已经奏效。使用@Arun的建议,我现在可以成功地设置空值。我认为我需要使用与我们的CRM版本相匹配的Xrm.Tooling。在将其投入生产之前,我将运行更多的测试和场景。仍然回避一个问题,那就是我使用的版本应该如何实现它!