これらのコード サンプルでは、グローバルな選択肢を挿入、更新、削除、並べ替える方法を示します。
新しい選択肢を挿入する
次の静的 InsertOptionValue メソッドは、 InsertOptionValueRequestを使用してグローバルな選択肢に新しい選択肢を追加する方法を示しています。
static int InsertOptionValue(
IOrganizationService service,
string globalOptionSetName,
Label value)
{
var request = new InsertOptionValueRequest
{
OptionSetName = globalOptionSetName,
Label = value
};
int newOptionValue = ((InsertOptionValueResponse)service.Execute(request)).NewOptionValue;
// Publish the OptionSet
service.Execute(new PublishXmlRequest
{
ParameterXml = string.Format(
"<importexportxml><optionsets><optionset>{0}</optionset></optionsets></importexportxml>",
globalOptionSetName)
});
return newOptionValue;
}
選択肢を更新する
次の静的 UpdateOptionValue メソッドは、 UpdateOptionValueRequestを使用してグローバルな選択肢の選択肢を更新する方法を示しています。
static void UpdateOptionValue(
IOrganizationService service,
string globalOptionSetName,
int value,
Label label)
{
service.Execute(new UpdateOptionValueRequest
{
OptionSetName = globalOptionSetName,
Value = value,
Label = label
});
// Publish the OptionSet
service.Execute(new PublishXmlRequest
{
ParameterXml = string.Format(
"<importexportxml><optionsets><optionset>{0}</optionset></optionsets></importexportxml>",
globalOptionSetName)
});
}
選択肢を削除する
次の静的 DeleteOptionValue メソッドは、 DeleteOptionValueRequestを使用してグローバルな選択肢の選択肢を削除する方法を示しています。
static void DeleteOptionValue(
IOrganizationService service,
string globalOptionSetName,
int value)
{
service.Execute(new DeleteOptionValueRequest
{
OptionSetName = globalOptionSetName,
Value = value
});
}
選択肢の順序
次の静的 OrderOptions メソッドは、 OrderOptionRequestを使用してグローバル選択の選択肢の順序を設定する方法を示しています。
static void OrderOptions(
IOrganizationService service,
string globalOptionSetName,
IEnumerable<OptionMetadata> updateOptionList)
{
service.Execute(new OrderOptionRequest
{
OptionSetName = globalOptionSetName,
// Use Select linq function to get only values in an array from the option list.
Values = updateOptionList.Select(x => x.Value.Value).ToArray()
});
// Publish the OptionSet
service.Execute(new PublishXmlRequest
{
ParameterXml = string.Format(
"<importexportxml><optionsets><optionset>{0}</optionset></optionsets></importexportxml>",
globalOptionSetName)
});
}
次の例は、静的 OrderOptions メソッドを使用して、ラベルのテキストに基づいて一連のオプションを並べ替える方法を示しています。
// Change the order of the original option's list.
// Use the OrderBy (OrderByDescending) linq function to sort options in
// ascending 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();
OrderOptions(service, _globalOptionSetName, updateOptionList)