Partilhar via


Inserir, atualizar, excluir e ordenar opções globais

Esses exemplos de código mostram como inserir, atualizar, excluir e ordenar opções globais.

Inserir uma nova opção

O seguinte método estático InsertOptionValue mostra como adicionar uma nova escolha às escolhas globais usando 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;
}

Atualizar uma opção

O método estático UpdateOptionValue a seguir mostra como atualizar uma opção nas escolhas globais usando 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)
    });
}

Excluir uma opção

O método estático DeleteOptionValue seguir mostra como excluir uma escolha nas escolhas globais usando DeleteOptionValueRequest:

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

Ordem das opções

O método estático OrderOptions a seguir mostra como definir a ordem das opções nas escolhas globais usando 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)
    });
}

O exemplo a seguir mostra o uso do método estático OrderOptions para ordenar um conjunto de opções com base no texto do rótulo.

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