【C#】【Dynamics CRM】Metadataの取得
Dynamics CRMのプライマリフィールドを調べたい! とうことで、Metadata取得周りを漁っていたのですが、随分めんどくさかったので、簡単にまとめました。
概要
まず理解する必要があるのは【MetadataFilterExpression】【MetadataPropertiesExpresion】の2つのクラス。
MetadataFilterExpressionでは取得するエンティティの指定もしくは取得するフィールドの指定
MetadataPropertiesExpresion上記で取得する予定のエンティティ、もしくはフィールドのどのプロパティを取得してくるかを指定します。
上記2つのインスタンスを最終的に【EntityQueryExpression】のプロパティへ設定し、RetrieveMetadataChangesRequestでDynamics CRMへExecuteするというものです。
他にもいろいろと指定できる要素はありますが、指定しているとえらく長くなってしまうので、今回は要所だけを抑えて、書いてみました。 (そもそも自分の理解が中途半端だったので、一旦整理した感じ)
コード
using System; using Microsoft.Xrm.Client; using Microsoft.Xrm.Client.Services; using Microsoft.Xrm.Sdk.Metadata.Query; using Microsoft.Xrm.Sdk.Query; using Microsoft.Xrm.Sdk.Messages; using Microsoft.Xrm.Sdk.Metadata; namespace MyProject.GetMetadata { class Program { static void Main(string[] args) { Console.WriteLine("CrmConnection Start!"); var connection = CrmConnection.Parse("Url=https://****.crm7.dynamics.com; Username=****@****.onmicrosoft.com; Password=****;"); var service = new OrganizationService(connection); // 取得するエンティティの指定フィルター // Entity名やユーザー所有のエンティティかどうかなどを指定する。 // 今回は取引先企業エンティティを取得する。 MetadataFilterExpression EntityFilter = new MetadataFilterExpression(LogicalOperator.Or); EntityFilter.Conditions.Add(new MetadataConditionExpression("LogicalName", MetadataConditionOperator.Equals, "account")); // 取得するEntityプロパティを指定 // 今回はすべて取得 MetadataPropertiesExpression EntityProperties = new MetadataPropertiesExpression() { AllProperties = true }; // 取得するフィールドの指定フィルター // 今回はPrimaryKeyとLookupFieldを取得 MetadataConditionExpression[] primaryAttributeTypes = new MetadataConditionExpression[] { new MetadataConditionExpression("IsPrimaryName", MetadataConditionOperator.Equals, true), new MetadataConditionExpression("AttributeType", MetadataConditionOperator.Equals, AttributeTypeCode.Lookup) }; MetadataFilterExpression AttributeFilter = new MetadataFilterExpression(LogicalOperator.Or); AttributeFilter.Conditions.AddRange(primaryAttributeTypes); // 取得するAttributeプロパティの指定 // 今回はすべて取得する。 MetadataPropertiesExpression AttributeProperties = new MetadataPropertiesExpression() { AllProperties = true }; // ラベル用のクエリを作成 今回は英語コード1033を直接指定 LabelQueryExpression labelQuery = new LabelQueryExpression(); labelQuery.FilterLanguages.Add(1033); // 最後にEntityQueryExpressionに作成したExpressionをすべて指定 EntityQueryExpression entityQueryExpression = new EntityQueryExpression() { Criteria = EntityFilter, // エンティティのフィルター Properties = EntityProperties, // エンティティのプロパティ指定 AttributeQuery = new AttributeQueryExpression() { Criteria = AttributeFilter, // フィールドのフィルター Properties = AttributeProperties // フィールドのプロパティの指定 }, LabelQuery = labelQuery // 表示言語の指定 }; // ExecuteにRetrieveMetadataChangesRequestインスタンスで受け渡す。 RetrieveMetadataChangesResponse respons = (RetrieveMetadataChangesResponse)service.Execute(new RetrieveMetadataChangesRequest() { Query = entityQueryExpression } ); foreach(AttributeMetadata metadata in respons.EntityMetadata[0].Attributes) { Console.WriteLine(string.Format("FieldName : {0} , Primary : {1} , Type : {2}", metadata.SchemaName, metadata.IsPrimaryName, metadata.AttributeTypeName)); } Console.ReadKey(); } } }