2013-06-04 55 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