だれかがオプションセットの値ではなく表示名が取得したいといっていたので、ひさびさにプログラム作ってみました。

サンプルでは、グローバルオプションセットのメタデータを取得して表示名を取得する方法と、エンティティ固有のオプションセットのメタデータを取得してオプションセットの各値に対応する表示名を取得する方法を実装ています。

動作確認環境

  • 動作環境:Dynamics CRM 2011 UR 6 オールインワン環境
  • Visual Studio 2010 Professional

1. サンプルプログラム

簡単なプログラムなので、Dynamics CRM 2011 の開発をある程度理解していれば難しくはないと思います。RetrieveEntityOptionSet メソッドでは取引先企業の支払方法のオプションセットの表示名を取得しています。RetrieveAllOptionSet メソッドでは グローバルオプションセットを取得しています。コメントでカスタマイズ可能な場合の条件も記載しています。RetrieveOptionSet メソッドでは特定の名前のグローバルオプションセットを取得しています。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk.Client;
using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Metadata;

namespace RetrievePicklistDisplayValue
{
    class Program
    {
        static void Main(string[] args)
        {
            using (OrganizationServiceProxy proxy = GetProxy())
            {
                // エンティティのオプションセットの表示名を取得する
                RetrieveEntityOptionSet(proxy);
                // グローバルなオプションセットの表示名を取得する
                RetrieveOptionSet(proxy);
                // 全てのグローバルオプションセットを取得する
                RetrieveAllOptionSet(proxy);

            }
            Console.ReadLine();
        }

        /// <summary>
        /// エンティティ固有のオプションセットを取得するサンプル
        /// </summary>
        /// <param name="service"></param>
        private static void RetrieveEntityOptionSet(IOrganizationService service)
        {
            // 取引先企業の支払い条件属性のピックリストの表示名を取得する
            RetrieveAttributeRequest request = new RetrieveAttributeRequest()
            {
                EntityLogicalName = "account",
                LogicalName = "paymenttermscode",
                RetrieveAsIfPublished = false
            };

            RetrieveAttributeResponse response = service.Execute(request) as RetrieveAttributeResponse;
            PicklistAttributeMetadata metadata = response.AttributeMetadata as PicklistAttributeMetadata;
            if (metadata != null)
            {
                foreach (var opt in metadata.OptionSet.Options)
                {
                    Console.WriteLine("{0}-{1}", opt.Value, opt.Label.UserLocalizedLabel.Label);
                }
            }
        }

        /// <summary>
        /// すべてのグローバルオプションセットを取得する
        /// </summary>
        /// <param name="service"></param>
        private static void RetrieveAllOptionSet(IOrganizationService service)
        {
            RetrieveAllOptionSetsRequest request = new RetrieveAllOptionSetsRequest()
            {
                RetrieveAsIfPublished = false
            };

            RetrieveAllOptionSetsResponse response = service.Execute(request) as RetrieveAllOptionSetsResponse;
            foreach (var item in response.OptionSetMetadata)
            //foreach (var item in response.OptionSetMetadata.Where(x => x.IsCustomizable.Value == true))
            {
                Console.WriteLine(item.DisplayName.UserLocalizedLabel.Label);
                OptionSetMetadata meta = item as OptionSetMetadata;
                // RetrieveOptionSetと同様にピックリストの表示名を取得可能
            }
        }

        /// <summary>
        /// 名前で指定したグローバルオプションセットの表示名を取得する
        /// </summary>
        /// <param name="service"></param>
        private static void RetrieveOptionSet(IOrganizationService service)
        {
            RetrieveOptionSetRequest request = new RetrieveOptionSetRequest
            {
                Name = "connectionrole_category"
            };

            RetrieveOptionSetResponse response = service.Execute(request) as RetrieveOptionSetResponse;
            OptionSetMetadata metadata = response.OptionSetMetadata as OptionSetMetadata;
            foreach (var opt in metadata.Options)
            {
                Console.WriteLine("{0}-{1}", opt.Value, opt.Label.UserLocalizedLabel.Label);
            }
        }
        private static OrganizationServiceProxy GetProxy()
        {
            Uri uri = new Uri("http://localhost/dyn/XRMServices/2011/Organization.svc");
            ClientCredentials client = new ClientCredentials();
            OrganizationServiceProxy proxy = new OrganizationServiceProxy(uri, null, client, null);
            proxy.EnableProxyTypes();
            return new OrganizationServiceProxy(uri, null, client, null);

        }
    }
}

説明は以上です。処理は簡単です。プラグインなどで同様の表示名を取得する処理を実装する場合は、表示名を静的変数に保存すればパフォーマンスの向上を図ることができます。