尝试从CRM 2011 Web服务接收信息时出错 [英] Error when trying to receive Information from CRM 2011 Webservice
问题描述
尝试从Dynamics CRM 2011检索系统用户信息时收到错误。以下代码有效:
I'm receiving an error while trying to retrieve systemuser information from Dynamics CRM 2011. The following Code works:
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时服务我收到一条错误消息,指出:
However if I try to add "businessunitid" to the ColumnSet when I invoke the service I get an error stating:
第1行位置1879错误。元素\ 2004/07 / System.Collections.Generic: value\'包含来自映射到名称\'/ xrm / 2011 / Contracts:OptionSetValue\'的类型的数据。解串器不知道任何映射到该名称的类型。请考虑使用DataContractResolver或添加与已知类型列表中的'OptionSetValue\'相对应的类型-例如,通过使用KnownTypeAttribute属性或将其添加到传递给DataContractSerializer的已知类型列表中。\'
"Error in line 1 position 1879. Element \ 2004/07/System.Collections.Generic:value\' contains data from a type that maps to the name \'/xrm/2011/Contracts:OptionSetValue\'. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver or add the type corresponding to \'OptionSetValue\' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.\'"
此错误是因为根据元数据信息。我尝试在 [数据合约]
标记下添加 [KnownType(typeof(OptionSetValue))]
无济于事,我已经Googling和Binging(?)这两天了,所以如果已经回答了,我表示歉意。
This error is because the data being returned is the type "Lookup" according to the metadata information. I tried adding [KnownType(typeof(OptionSetValue))]
just under the [Data Contract]
tag to no avail and I've been Googling and Binging(?) this for two days now so if it's already been answered I apologize.
推荐答案
我终于找到了。您必须打开自动生成的Reference.cs代码。搜索实体类:
I finally found it. You have to open the auto generated Reference.cs code. Do a search for the entity class:
public partial class Entity : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {
转到其上方的行并添加已知类型的东西,以便实体可以取消
Go to the line above it and add the known type stuff so the entity can de-serialize it.
[System.Runtime.Serialization.KnownTypeAttribute(typeof(EntityReference))]
[System.Runtime.Serialization.KnownTypeAttribute(typeof(OptionSetValue))]
public partial class Entity : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {
这篇关于尝试从CRM 2011 Web服务接收信息时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!