Share via


グローバル オプション セットのオプションの挿入、更新、削除、および並べ替え

公開日: 2017年1月

対象: Dynamics 365 (online)、Dynamics 365 (on-premises)、Dynamics CRM 2016、Dynamics CRM Online

次のコード サンプルに、グローバル オプション セットのオプションの挿入、更新、削除、および並べ替えの方法を示します。

このトピックの内容

新しいオプションの挿入

オプションの更新

オプションの削除

オプションの並べ替え

新しいオプションの挿入

次のサンプルに、InsertOptionValueRequest を使用してグローバル オプション セットに新しいオプションを追加する方法を示します。


// Use InsertOptionValueRequest to insert a new option into a 
// global option set.
InsertOptionValueRequest insertOptionValueRequest =
    new InsertOptionValueRequest
    {
        OptionSetName = _globalOptionSetName,
        Label = new Label("New Picklist Label", _languageCode)
    };

// Execute the request and store the newly inserted option value 
// for cleanup, used in the later part of this sample.
_insertedOptionValue = ((InsertOptionValueResponse)_serviceProxy.Execute(
    insertOptionValueRequest)).NewOptionValue;

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

' Use InsertOptionValueRequest to insert a new option into a 
' global option set.
Dim insertOptionValueRequest As InsertOptionValueRequest = New InsertOptionValueRequest With {
 .OptionSetName = _globalOptionSetName,
 .Label = New Label("New Picklist Label", _languageCode)
}

' Execute the request and store the newly inserted option value 
' for cleanup, used in the later part of this sample.
_insertedOptionValue =
 (CType(_serviceProxy.Execute(insertOptionValueRequest), InsertOptionValueResponse)).NewOptionValue

'Publish the OptionSet
Dim pxReq2 As PublishXmlRequest = New PublishXmlRequest With {
 .ParameterXml = String.Format("<importexportxml><optionsets><optionset>{0}</optionset></optionsets></importexportxml>", _globalOptionSetName)
}
_serviceProxy.Execute(pxReq2)

オプションの更新

次のサンプルに、UpdateOptionValueRequest を使用してグローバル オプション セットのオプションを更新する方法を示します。


// In order to change labels on option set values (or delete) option set
// values, you must use UpdateOptionValueRequest 
// (or DeleteOptionValueRequest).
UpdateOptionValueRequest updateOptionValueRequest =
    new UpdateOptionValueRequest
    {
        OptionSetName = _globalOptionSetName,
        // Update the second option value.
        Value = optionList[1].Value.Value,
        Label = new Label("Updated Option 1", _languageCode)
    };

_serviceProxy.Execute(updateOptionValueRequest);

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

' In order to change labels on option set values (or delete) option set
' values, you must use UpdateOptionValueRequest 
' (or DeleteOptionValueRequest).
Dim updateOptionValueRequest As UpdateOptionValueRequest =
 New UpdateOptionValueRequest With {
  .OptionSetName = _globalOptionSetName,
  .Value = optionList(1).Value.Value,
  .Label = New Label("Updated Option 1", _languageCode)
 }
' Update the second option value.

_serviceProxy.Execute(updateOptionValueRequest)

'Publish the OptionSet
Dim pxReq3 As PublishXmlRequest =
 New PublishXmlRequest With {
  .ParameterXml =
  String.Format("<importexportxml><optionsets><optionset>{0}</optionset></optionsets></importexportxml>",
                _globalOptionSetName)
 }
_serviceProxy.Execute(pxReq3)

オプションの削除

次のサンプルに、DeleteOptionValueRequest を使用してグローバル オプション セットのオプションを削除する方法を示します。


// Use the DeleteOptionValueRequest message 
// to remove the newly inserted label.
DeleteOptionValueRequest deleteOptionValueRequest =
    new DeleteOptionValueRequest
{
    OptionSetName = _globalOptionSetName,
    Value = _insertedOptionValue
};

// Execute the request.
_serviceProxy.Execute(deleteOptionValueRequest);

' Use the DeleteOptionValueRequest message 
' to remove the newly inserted label.
Dim deleteOptionValueRequest As DeleteOptionValueRequest =
 New DeleteOptionValueRequest With {
  .OptionSetName = _globalOptionSetName,
  .Value = _insertedOptionValue
 }

' Execute the request.
_serviceProxy.Execute(deleteOptionValueRequest)

オプションの並べ替え

次のサンプルに、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
_serviceProxy.Execute(orderOptionRequest);

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

' 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:
Dim updateOptionList = optionList.OrderBy(Function(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.
Dim orderOptionRequest As OrderOptionRequest =
 New OrderOptionRequest With {
  .OptionSetName = _globalOptionSetName,
  .Values = updateOptionList.Select(Function(x) x.Value.Value).ToArray()
 }
' Set the properties for the request.
' Set the changed order using Select linq function 
' to get only values in an array from the changed option list.

' Execute the request
_serviceProxy.Execute(orderOptionRequest)

'Publish the OptionSet
Dim pxReq4 As PublishXmlRequest =
 New PublishXmlRequest With {
  .ParameterXml = String.Format("<importexportxml><optionsets><optionset>{0}</optionset></optionsets></importexportxml>",
                                _globalOptionSetName)
 }
_serviceProxy.Execute(pxReq4)

関連項目

グローバル オプション セットのカスタマイズ
グローバル オプション セットのメッセージ
グローバル オプション セットのメタデータ値

Microsoft Dynamics 365

© 2017 Microsoft. All rights reserved. 著作権