我在尝试从 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。\'"
这个错误是因为返回的数据根据元数据信息是“查找”类型。我尝试[KnownType(typeof(OptionSetValue))]
在[Data Contract]
标签下添加无济于事,我已经在谷歌搜索和 Binging(?)这两天了,所以如果它已经得到回答,我很抱歉。