2013-06-04 45 views
4

尝试从Dynamics CRM中检索systemuser信息我收到一个错误2011.下面的代码工作:错误试图从CRM接收信息时,2011 Webservice的

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:值\”包含从类型数据映射到名称\'/ xrm/2011/Contracts:OptionSetValue \'。反序列化程序不知道映射的任何类型到这个名字。考虑使用DataContractResolver或将与'OptionSetValue'相对应的类型添加到已知类型的列表中 - 例如,通过使用KnownTypeAttribute属性或将其添加到传递给DataContractSerializer的已知类型的列表中。''

此错误是因为根据metadata information正在返回的数据是类型“查找”。我试图在[Data Contract]标记下添加[KnownType(typeof(OptionSetValue))]无效,我已经两天了谷歌和Binging(?)所以如果它已经被回答道歉

+0

好奇心:您在代码中使用哪些CRM程序集?来自crm 2011 sdk? CrmUser是一个自定义类? –

+0

是的,CrmUser是一个自定义类,包含4个字符串。名字,姓氏,用户id,userGuid。我正在使用Microsoft.Xrm.Sdk程序集版本5.0.0.0。 – EricKitt

+0

你可以请指定你的应用程序的类型(silverlight,windows窗体,web服务),如果silverlight如果运行在web内部crm或不是 –

回答

0

我终于找到了。你必须打开自动生成的Reference.cs代码。搜索实体类:

public partial class Entity : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged { 

转到上面的行并添加已知类型的东西,以便实体可以反序列化它。

[System.Runtime.Serialization.KnownTypeAttribute(typeof(EntityReference))] 
[System.Runtime.Serialization.KnownTypeAttribute(typeof(OptionSetValue))] 
public partial class Entity : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged { 
+0

我还发现,您还必须在每次更新或更改服务参考时重新添加它。 – EricKitt

0

只是杠杆Microsoft.Xrm.Client。它为您提供CrmConnection,一个简化的方法conn等系统。

我也建议(但它可能更多的是品味/需求)总是通过GetAttributeValue<>读取属性以获得更多可读代码。

全部工作样品,它包括BU ID:

// helper class 
public static class CommonCrm 
{   
    public static readonly CrmConnection crmConnection = null; 
    public static readonly OrganizationService crmService = null; 
    public static readonly CrmOrganizationServiceContext crmContext = null; 

    static CommonCrm() 
    { 
     try 
     { 
      CommonCrm.crmConnection = new CrmConnection("Crm"); 

// "Crm" is a connection string which goes like this in Web.config: 
//<connectionStrings> 
// <add name="Crm" connectionString="Url=http://orgUrl; Domain=X; Username=Y; Password=Z;" /> 
//</connectionStrings> 

      CommonCrm.crmService = new OrganizationService(crmConnection); 
      CommonCrm.crmContext = new CrmOrganizationServiceContext(crmService); 
     } 
     catch (Exception ex) 
     { 
      //Log exception (code removed) 
      throw; 
     } 
    } 
} 

public class CrmUser 
{ 
    public string firstName { get; set; } 
    public string lastName { get; set; } 
    public string userId { get; set; } 
    public Guid userGuid { get; set; } 
    public Guid buId { get; set; } 

    public static List<CrmUser> GetAllCrmUsers() 
    { 
     List<CrmUser> CrmUsers = new List<CrmUser>(); 
     try 
     { 
      ColumnSet colsPrincipal = new ColumnSet("lastname", "firstname", "domainname", "systemuserid", "businessunitid"); 
      QueryExpression queryPrincipal = new QueryExpression(); 
      queryPrincipal.EntityName = "systemuser"; 
      queryPrincipal.ColumnSet = colsPrincipal; 

      var myAccounts = CommonCrm.crmContext.RetrieveMultiple(queryPrincipal); 

      foreach (var myEntity in myAccounts.Entities) 
      { 
       //create new crm users and add it to the list 
       CrmUser thisOne = new CrmUser(); 

       thisOne.firstName = myEntity.GetAttributeValue<string>("firstname"); 
       thisOne.lastName = myEntity.GetAttributeValue<string>("name"); 
       thisOne.userId = myEntity.GetAttributeValue<string>("domainname"); 
       thisOne.userGuid = myEntity.GetAttributeValue<Guid>("systemuserid"); 
       thisOne.buId = myEntity.GetAttributeValue<EntityReference>("businessunitid").Id; 
       // and so on and so forth...   

       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; 
    } 
} 
+0

他们正在重新加载开发人员CRM服务器,并且它已关闭,所以我没有机会尝试您的建议。感谢您的意见,但我一定会在备份后发布更多内容。 – EricKitt