選択肢をカスタマイズする

通常、グローバル の選択肢 (オプションセット) を使用してフィールドを設定して、異なるフィールドが同じオプションのセットを共有できるようにします。これは 1 つの場所で維持されます。 特定のテーブル列に対してのみ定義されている ローカル の選択肢とは異なり、グローバル選択を再利用できます。 要求パラメーターの中で列挙体と同じように使われる例もあります。

注意

マネージド ソリューションの発行者のみが、グローバル オプション セットからオプションを削除する変更をインポートできます。 これには、標準のグローバル オプション セットなど、Microsoft が公開しているソリューションが含まれます。 オプション セットに変更を加えるには、オプション セットを追加したソリューションにアップグレードする必要があります。 詳細については、ソリューションのアップグレードまたは更新をご参照ください。 ソリューションの変更や、ソリューション発行者への連絡ができない場合、ユーザーは環境内のオプションを手動で削除できますが、これはすべての環境で手動で行う必要があります。

CreateOptionSetRequest を使用してグローバルな選択肢を定義する場合、システムに値を割り当てさせることをお勧めします。 具体的には新規の OptionMetadata インスタンスを作成するとき、引数として null 値を渡します。 選択肢を定義すると、選択肢が作成されたソリューションのパブリッシャー セットのコンテキストに固有の選択肢値プレフィックスが含まれます。 この接頭辞は、管理ソリューション、管理ソリューションがインストールされている組織で定義されている選択肢の重複を作成する可能性を減らすのに役立ちます。 詳細については、マージ選択オプション を参照してください。

コード サンプルは、こちら からダウンロードできます。

メッセージ要求クラス

次のメッセージ要求クラスを使用して、グローバルな選択肢を操作します。

次のメッセージ要求クラスを使用して、グローバルとローカルの両方の選択肢を操作します。

グローバルな選択肢を取得する

次のコード サンプルは、RetrieveOptionSetRequest メッセージを使用して名前でグローバル選択肢を取得する方法を示しています。

// Use the RetrieveOptionSetRequest message to retrieve
// a global option set by it's name.
RetrieveOptionSetRequest retrieveOptionSetRequest =
    new RetrieveOptionSetRequest
    {
        Name = _globalOptionSetName
    };

// Execute the request.
RetrieveOptionSetResponse retrieveOptionSetResponse =
    (RetrieveOptionSetResponse)svc.Execute(
    retrieveOptionSetRequest);

Console.WriteLine("Retrieved {0}.",
    retrieveOptionSetRequest.Name);

// Access the retrieved OptionSetMetadata.
OptionSetMetadata retrievedOptionSetMetadata =
    (OptionSetMetadata)retrieveOptionSetResponse.OptionSetMetadata;

// Get the current options list for the retrieved attribute.
OptionMetadata[] optionList =
    retrievedOptionSetMetadata.Options.ToArray();

グローバルな選択肢の作成

CreateOptionSetRequest メッセージを使用して、新しいグローバルな選択肢を作成します。 IsGlobal プロパティを true に設定して、選択がグローバルであることを示します。 次のコード例は、「Example オプション セット」 というグローバルな選択肢を作成します:

// Define the request object and pass to the service.
CreateOptionSetRequest createOptionSetRequest = new CreateOptionSetRequest
{
    // Create a global option set (OptionSetMetadata).
    OptionSet = new OptionSetMetadata
    {
        Name = _globalOptionSetName,
        DisplayName = new Label("Example Option Set", _languageCode),
        IsGlobal = true,
        OptionSetType = OptionSetType.Picklist,
        Options =
    {
        new OptionMetadata(new Label("Open", _languageCode), null),
        new OptionMetadata(new Label("Suspended", _languageCode), null),
        new OptionMetadata(new Label("Cancelled", _languageCode), null),
        new OptionMetadata(new Label("Closed", _languageCode), null)
    }
    }
};

// Execute the request.
CreateOptionSetResponse optionsResp =
    (CreateOptionSetResponse)svc.Execute(createOptionSetRequest);

グローバルな選択肢を使用する選択肢を作成する

次のサンプルは、CreateAttributeRequest を使用してグローバル選択を使用する選択列を作成する方法を示しています。

// Create a Picklist linked to the option set.
// Specify which entity will own the picklist, and create it.
CreateAttributeRequest createRequest = new CreateAttributeRequest
{
    EntityName = Contact.EntityLogicalName,
    Attribute = new PicklistAttributeMetadata
    {
        SchemaName = "sample_examplepicklist",
        LogicalName = "sample_examplepicklist",
        DisplayName = new Label("Example Picklist", _languageCode),
        RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),

        // In order to relate the picklist to the global option set, be sure
        // to specify the two attributes below appropriately.
        // Failing to do so will lead to errors.
        OptionSet = new OptionSetMetadata
        {
            IsGlobal = true,
            Name = _globalOptionSetName
        }
    }
};

svc.Execute(createRequest);

グローバルな選択肢を更新する

次のコード サンプルは、UpdateOptionSetRequest メッセージを使用してラベルでグローバル選択肢を更新する方法を示しています。

// Use UpdateOptionSetRequest to update the basic information of an option
// set. Updating option set values requires different messages (see below).
UpdateOptionSetRequest updateOptionSetRequest = new UpdateOptionSetRequest
{
    OptionSet = new OptionSetMetadata
    {
        DisplayName = new Label("Updated Option Set", _languageCode),
        Name = _globalOptionSetName,
        IsGlobal = true
    }
};

svc.Execute(updateOptionSetRequest);

//Publish the OptionSet
PublishXmlRequest pxReq1 = new PublishXmlRequest { ParameterXml = String.Format("<importexportxml><optionsets><optionset>{0}</optionset></optionsets></importexportxml>", _globalOptionSetName) };
svc.Execute(pxReq1);

オプションの順序設定

次のコード サンプルは、グローバルな選択肢のオプションが OrderOptionRequest を使用して順に並べる方法を示しています。

// Change the order of the original option's list.
// Use the OrderBy (OrderByDescending) linq function to sort options in
// ascending (descending) order according to label text.
// For ascending order use this:
var updateOptionList =
    optionList.OrderBy(x => x.Label.LocalizedLabels[0].Label).ToList();

// For descending order use this:
// var updateOptionList =
//      optionList.OrderByDescending(
//      x => x.Label.LocalizedLabels[0].Label).ToList();

// Create the request.
OrderOptionRequest orderOptionRequest = new OrderOptionRequest
{
    // Set the properties for the request.
    OptionSetName = _globalOptionSetName,
    // Set the changed order using Select linq function
    // to get only values in an array from the changed option list.
    Values = updateOptionList.Select(x => x.Value.Value).ToArray()
};

// Execute the request
svc.Execute(orderOptionRequest);

//Publish the OptionSet
PublishXmlRequest pxReq4 = new PublishXmlRequest {
ParameterXml = String.Format("<importexportxml><optionsets><optionset>{0}</optionset></optionsets></importexportxml>", _globalOptionSetName)
};
svc.Execute(pxReq4);

すべてのグローバルな選択肢を取得する

次のコード サンプルは、RetrieveAllOptionSetsRequest メッセージを使用して名前でグローバル選択肢を取得する方法を示しています。

// Use RetrieveAllOptionSetsRequest to retrieve all global option sets.
// Create the request.
RetrieveAllOptionSetsRequest retrieveAllOptionSetsRequest =
    new RetrieveAllOptionSetsRequest();

// Execute the request
RetrieveAllOptionSetsResponse retrieveAllOptionSetsResponse =
    (RetrieveAllOptionSetsResponse)svc.Execute(
    retrieveAllOptionSetsRequest);

// Now you can use RetrieveAllOptionSetsResponse.OptionSetMetadata property to
// work with all retrieved option sets.
if (retrieveAllOptionSetsResponse.OptionSetMetadata.Count() > 0)
{
    Console.WriteLine("All the global option sets retrieved as below:");
    int count = 1;
    foreach (OptionSetMetadataBase optionSetMetadata in
        retrieveAllOptionSetsResponse.OptionSetMetadata)
    {
        Console.WriteLine("{0} {1}", count++,
            (optionSetMetadata.DisplayName.LocalizedLabels.Count >0)? optionSetMetadata.DisplayName.LocalizedLabels[0].Label : String.Empty);
    }
}

グローバルな選択肢の削除

次のコードサンプルは、グローバルな選択肢が RetrieveDependentComponents メッセージ (RetrieveDependentComponents Function または RetrieveDependentComponentsRequest) を使用することによって別のソリューション コンポーネントによって使用されているかどうかを確認し方法と、その後 DeleteOptionSet メッセージ (.NET 用 SDK の場合は、DeleteOptionSetRequest を使う) を使用することで削除する方法について示します。

// Create the request to see which components have a dependency on the
// global option set.
RetrieveDependentComponentsRequest dependencyRequest =
    new RetrieveDependentComponentsRequest
    {
        ObjectId = _optionSetId,
        ComponentType = (int)componenttype.OptionSet
    };

RetrieveDependentComponentsResponse dependencyResponse =
    (RetrieveDependentComponentsResponse)svc.Execute(
    dependencyRequest);

// Here you would check the dependencyResponse.EntityCollection property
// and act as appropriate. However, we know there is exactly one
// dependency so this example deals with it directly and deletes
// the previously created attribute.
DeleteAttributeRequest deleteAttributeRequest =
    new DeleteAttributeRequest
{
    EntityLogicalName = Contact.EntityLogicalName,
    LogicalName = "sample_examplepicklist"
};

svc.Execute(deleteAttributeRequest);

Console.WriteLine("Referring attribute deleted.");

// Finally, delete the global option set. Attempting this before deleting
// the picklist above will result in an exception being thrown.
DeleteOptionSetRequest deleteRequest = new DeleteOptionSetRequest
{
    Name = _globalOptionSetName
};

svc.Execute(deleteRequest);

参照

Web API を使用して選択肢を作成および更新する

注意

ドキュメントの言語設定についてお聞かせください。 簡単な調査を行います。 (この調査は英語です)

この調査には約 7 分かかります。 個人データは収集されません (プライバシー ステートメント)。