【发布时间】:2013-05-31 01:27:55
【问题描述】:
我在尝试从 Dynamics CRM 2011 检索系统用户信息时收到错误消息。以下代码有效:
public List<CrmUser> GetAllCrmUsers()
{
List<CrmUser> CrmUsers = new List<CrmUser>();
using (CrmSdk.OrganizationServiceClient myCrm = new CrmSdk.OrganizationServiceClient("CustomBinding_IOrganizationService1"))
{
try
{
// this will need to be changed... the address to a key in the app.config and the credentials will need to be whatever is correct for the
// end server to hit the CRM WCF service
myCrm.Endpoint.Address = new System.ServiceModel.EndpointAddress("https://devcrm.removed/XRMServices/2011/Organization.svc");
myCrm.ClientCredentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
CrmSdk.ColumnSet colsPrincipal = new CrmSdk.ColumnSet();
colsPrincipal.Columns = new string[] { "lastname", "firstname", "domainname", "systemuserid" };
CrmSdk.QueryExpression queryPrincipal = new CrmSdk.QueryExpression();
queryPrincipal.EntityName = "systemuser";
queryPrincipal.ColumnSet = colsPrincipal;
CrmSdk.EntityCollection myAccounts = myCrm.RetrieveMultiple(queryPrincipal);
foreach (CrmSdk.Entity myEntity in myAccounts.Entities)
{
//create new crm users and add it to the list
CrmUser thisOne = new CrmUser();
thisOne.firstName = myEntity.Attributes[0].Value.ToString();
thisOne.lastName = myEntity.Attributes[1].Value.ToString();
thisOne.userId = myEntity.Attributes[2].Value.ToString();
thisOne.userGuid = myEntity.Attributes[3].Value.ToString();
CrmUsers.Add(thisOne);
}
}
catch (Exception ex)
{
CrmUser thisOne = new CrmUser();
thisOne.firstName = "Crap there was an error";
thisOne.lastName = ex.ToString();
CrmUsers.Add(thisOne);
}
}
return CrmUsers;
}
但是,如果我在调用服务时尝试将“businessunitid”添加到 ColumnSet,我会收到一条错误消息:
“第 1 行位置 1879 出错。元素 \2004/07/System.Collections.Generic:value\' 包含来自映射到名称 \'/xrm/2011/Contracts:OptionSetValue\' 的类型的数据。解串器不知道映射到此名称的任何类型。请考虑使用 DataContractResolver 或将与 \'OptionSetValue\' 对应的类型添加到已知类型列表中 - 例如,通过使用 KnownTypeAttribute 属性或将其添加到列表中传递给 DataContractSerializer 的已知类型。\'"
此错误是因为返回的数据是根据metadata information 的“查找”类型。我尝试在[Data Contract] 标签下添加[KnownType(typeof(OptionSetValue))] 无济于事,我已经用谷歌搜索和Binging(?)这两天了,所以如果它已经得到回答,我很抱歉。
【问题讨论】:
-
好奇心:您在代码中使用了哪些 CRM 程序集?来自crm 2011 sdk? CrmUser 是自定义类?
-
是的,CrmUser 是一个自定义类,包含 4 个字符串。名字,姓氏,用户ID,用户Guid。我正在使用 Microsoft.Xrm.Sdk 程序集版本 5.0.0.0。
-
能否请您指定应用程序的类型(silverlight、Windows 窗体、Web 服务)以及是否在 crm 中作为 webresource 运行 silverlight
-
可能只需要添加 [KnownTypeAttribute(typeof(EntityReference))] 但我很想知道您为什么要使用这种与 CRM 的连接
-
我相信您的错误是由 CRM 返回 OptionSetValue 引起的,并且尝试在客户端反序列化它的代码没有对 Microsoft.Xrm.Sdk 的引用,但您已经提到确实如此,所以我有点困惑。
标签: c# wcf dynamics-crm-2011