Freigeben über


Einfügen, Aktualisieren, Löschen und Sortieren globaler Auswahlmöglichkeiten

In diesen Codebeispielen wird gezeigt, wie Sie globale Auswahlmöglichkeiten einfügen, aktualisieren, löschen und sortieren.

Einfügen einer neuen Auswahl

Die folgende statische InsertOptionValue Methode zeigt, wie Sie mithilfe von InsertOptionValueRequest eine neue Auswahl zu den globalen Optionen hinzufügen können.

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;
}

Aktualisieren einer Auswahl

Die folgende statische UpdateOptionValue Methode zeigt, wie Sie eine Auswahl in globalen Optionen mithilfe von UpdateOptionValueRequest aktualisieren:

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)
    });
}

Löschen einer Auswahl

Die folgende statische Methode DeleteOptionValue zeigt, wie Sie eine Auswahl in den globalen Auswahlmöglichkeiten mithilfe von DeleteOptionValueRequest löschen:

static void DeleteOptionValue(
    IOrganizationService service,
    string globalOptionSetName,
    int value)
{
    service.Execute(new DeleteOptionValueRequest
    {
        OptionSetName = globalOptionSetName,
        Value = value
    });
}

Reihenfolge der Auswahlmöglichkeiten

Die folgende statische OrderOptions Methode zeigt, wie die Reihenfolge der Optionen in globalen Auswahlmöglichkeiten mithilfe von OrderOptionRequest festgelegt wird.

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)
    });
}

Das folgende Beispiel zeigt die Verwendung der statischen OrderOptions Methode, um eine Reihe von Optionen basierend auf dem Text der Beschriftung zu ordnen.

// 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)