2009-06-30 12 views
4

CRMインストールから情報を取得しようとしていますが、これまでのところデフォルトフィールドを使用すると問題ありません。しかし、私はカスタムフィールドの取得に問題があります。たとえば、連絡先にはweb_usernameというカスタムフィールドがあります。Webサービスクエリを使用してDynamics CRMのカスタムフィールドを取得する

現時点での私のコードは

 QueryExpression query = new QueryExpression(); 
     query.EntityName = "contact"; 
     ColumnSet cols = new ColumnSet(); 
     cols.Attributes = new string[] { "firstname", "lastname" }; 
     query.ColumnSet = cols; 

     BusinessEntityCollection beReturned = tomService.RetrieveMultiple(query); 
     foreach (contact _contact in beReturned.BusinessEntities) 
     { 
      DataRow dr = dt.NewRow(); 
      dr["firstname"] = _contact.firstname; 
      dr["lastname"] = _contact.lastname; 
      dt.Rows.Add(dr); 
     } 

どのように私は私のクエリでカスタムフィールドが含まれないのですか?私は周りを検索しようとしましたが、運がまだありませんが、私はCRM用語に慣れていないので、誤って検索している可能性があります。

事前に乾杯!

+0

あなたの解決のために、このリンクを参照することができます。 [https://stackoverflow.com/questions/43984397/retrieve-fetch-records-from-dynamic-365-crm-using-c-sharp-code/43985297#43985297] [1] – Piyush

答えて

6

私はそれ以来これを解決できました。それが他の誰にとっても役に立つのであれば、これは私がしたことです。以前のように、カスタムフィールドをColumnSetに追加した以外は、クエリが設定されます。

cols.Attributes = new string[] { "firstname", "lastname", "new_web_username" }; 

そして真

 RetrieveMultipleResponse retrived = new RetrieveMultipleResponse(); 

     RetrieveMultipleRequest retrive = new RetrieveMultipleRequest(); 
     retrive.Query = query; 
     retrive.ReturnDynamicEntities = true; 

     retrived = (RetrieveMultipleResponse)tomService.Execute(retrive); 

に設定ReturnDynamicEntitiesでRetrieveMultipleResponseとリクエストを使用し、私はこれを行うためのより良い方法がある場合は、まだコメントしてください。連絡先

contact myContact = (contact)myService.Retrieve(EntityName.contact.ToString(), userID, cols); 

にキャストした場合、私の元の質問の例を使用

EDIT あなたは、オブジェクト

   phone = myContact.telephone1; 
      password = myContact.new_password; 

のアクセスプロパティは、あなたのCRMのwebreferenceカスタムを更新することができる場合CRMで追加したフィールドは使用可能です。ここ

0
public List<Entity> GetEntitiesCollection(IOrganizationService service, string entityName, ColumnSet col) 
    { 
         try 
         { 
          QueryExpression query = new QueryExpression 
          { 
           EntityName = entityName, 
           ColumnSet = col 
          }; 
          var testResult = service.RetrieveMultiple(query); 
          var testResultSorted = testResult.Entities.OrderBy(x => x.LogicalName).ToList(); 

      foreach (Entity res in testResultSorted) 
         { 
          var keySorted = res.Attributes.OrderBy(x => x.Key).ToList(); 
          DataRow dr = null; 
          dr = dt.NewRow(); 
          foreach (var attribute in keySorted) 
          { 
           try 
           { 
            if (attribute.Value.ToString() == "Microsoft.Xrm.Sdk.OptionSetValue") 
            { 
             var valueofattribute = GetoptionsetText(entityName, attribute.Key, ((Microsoft.Xrm.Sdk.OptionSetValue)attribute.Value).Value, _service); 
dr[attribute.Key] = valueofattribute; 


             } 
             else if (attribute.Value.ToString() == "Microsoft.Xrm.Sdk.EntityReference") 
             { 
              dr[attribute.Key] = ((Microsoft.Xrm.Sdk.EntityReference)attribute.Value).Name; 
             } 
             else 
             { 
              dr[attribute.Key] = attribute.Value; 
             } 
            } 
            catch (Exception ex) 
            { 
             Response.Write("<br/>optionset Error is :" + ex.Message); 
            } 
           } 
           dt.Rows.Add(dr); 
          } 
       return testResultSorted; 
       } 
         catch (Exception ex) 
         { 
          Response.Write("<br/> Error Message : " + ex.Message); 
          return null; 
         } 
      } 

//私は別の関数1を言及しています。この機能の

var valueofattribute = GetoptionsetText(entityName, attribute.Key, ((Microsoft.Xrm.Sdk.OptionSetValue)attribute.Value).Value, _service); 

//定義は以下の通りです:

public static string GetoptionsetText(string entityName, string attributeName, int optionSetValue, IOrganizationService service) 
    { 
       string AttributeName = attributeName; 
     string EntityLogicalName = entityName; 
     RetrieveEntityRequest retrieveDetails = new RetrieveEntityRequest 
     { 
      EntityFilters = EntityFilters.All, 
      LogicalName = entityName 
     }; 
     RetrieveEntityResponse retrieveEntityResponseObj = (RetrieveEntityResponse)service.Execute(retrieveDetails); 
     Microsoft.Xrm.Sdk.Metadata.EntityMetadata metadata = retrieveEntityResponseObj.EntityMetadata; 
     Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata picklistMetadata = metadata.Attributes.FirstOrDefault(attribute => String.Equals(attribute.LogicalName, attributeName, StringComparison.OrdinalIgnoreCase)) as Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata; 
     Microsoft.Xrm.Sdk.Metadata.OptionSetMetadata options = picklistMetadata.OptionSet; 

     IList<OptionMetadata> OptionsList = (from o in options.Options 
              where o.Value.Value == optionSetValue 
              select o).ToList(); 
     string optionsetLabel = (OptionsList.First()).Label.UserLocalizedLabel.Label; 
     return optionsetLabel; 
    } 
関連する問題