Condividi tramite


Personalizzare set di opzioni globali

 

Data di pubblicazione: gennaio 2017

Si applica a: Dynamics 365 (online), Dynamics 365 (on-premises), Dynamics CRM 2016, Dynamics CRM Online

In genere, utilizzare set di opzioni globali per impostare i campi in modo che campi diversi possano condividere lo stesso set di opzioni, che vengono gestite in una ubicazione. È possibile riutilizzare set di opzioni globali. Verranno anche visualizzati utilizzati nei parametri delle richieste in un modo simile all'enumerazione.

Durante la definizione un valore di opzione utilizzando OptionMetadata, è consigliabile consentire al sistema di assegnare un valore. Questo si ottiene passando un valore null quando si crea la nuova istanza OptionMetadata. Durante la definizione di un'opzione, conterrà un prefisso valore opzione nel contesto specifico dell'autore impostato per la soluzione in cui il set di opzioni viene creato. Questo prefisso consente di ridurre la probabilità di creare set di opzioni duplicati per una soluzione gestita e in tutti i set di opzioni che siano definiti nelle organizzazioni della soluzione gestita installata. Per ulteriori informazioni, vedere Unire le opzioni del set di opzioni.

In questo argomento

Recupera un set di opzioni globale

Creare un set di opzioni globale

Creare un elenco a discesa che utilizza un set di opzioni globale

Aggiornare un set di opzioni globale

Recuperare tutti i set di opzioni globali

Eliminare un set di opzioni globale

Recupera un set di opzioni globale

Nell'esempio seguente viene illustrato come recuperare un set di opzioni globale in base al nome utilizzando il messaggio 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)_serviceProxy.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();

' Use the RetrieveOptionSetRequest message to retrieve  
' a global option set by it's name.
Dim retrieveOptionSetRequest As RetrieveOptionSetRequest = New RetrieveOptionSetRequest With {
 .Name = _globalOptionSetName
}

' Execute the request.
Dim retrieveOptionSetResponse As RetrieveOptionSetResponse =
 CType(_serviceProxy.Execute(retrieveOptionSetRequest), RetrieveOptionSetResponse)

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

' Access the retrieved OptionSetMetadata.
Dim retrievedOptionSetMetadata As OptionSetMetadata =
 CType(retrieveOptionSetResponse.OptionSetMetadata, OptionSetMetadata)

' Get the current options list for the retrieved attribute.
Dim optionList() As OptionMetadata = retrievedOptionSetMetadata.Options.ToArray()

Creare un set di opzioni globale

Utilizzare il messaggio CreateOptionSetRequest per creare un nuovo set di opzioni globale. Impostare la proprietà di IsGlobal su true per indicare che il set di opzioni è globale. L'esempio di codice seguente crea un set di opzioni globale denominato "set di opzioni di esempio":


#region How to create global option set
// 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)_serviceProxy.Execute(createOptionSetRequest);

'                    #Region "How to create global option set"
' Define the request object and pass to the service.
Dim createOptionSetRequest As CreateOptionSetRequest = New CreateOptionSetRequest()
Dim createOptionSetOptionSet As OptionSetMetadata = New OptionSetMetadata() With {
 .Name = _globalOptionSetName,
 .DisplayName = New Label("Example Option Set", _languageCode),
 .IsGlobal = True,
 .OptionSetType = OptionSetType.Picklist
}
createOptionSetOptionSet.Options.AddRange(
 {
  New OptionMetadata(New Label("Open", _languageCode), Nothing),
  New OptionMetadata(New Label("Suspended", _languageCode), Nothing),
  New OptionMetadata(New Label("Cancelled", _languageCode), Nothing),
  New OptionMetadata(New Label("Closed", _languageCode), Nothing)
 }
)

createOptionSetRequest.OptionSet = createOptionSetOptionSet
' Create a global option set (OptionSetMetadata).

' Execute the request.
Dim optionsResp As CreateOptionSetResponse =
 CType(_serviceProxy.Execute(createOptionSetRequest), CreateOptionSetResponse)

Creare un elenco a discesa che utilizza un set di opzioni globale

Nell'esempio di seguito viene illustrato come creare un attributo di tipo picklist (elenco a discesa) che utilizza un set di opzioni globale utilizzando 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
        }
    }
};

_serviceProxy.Execute(createRequest);

' Create a Picklist linked to the option set.
' Specify which entity will own the picklist, and create it.
Dim createRequest As CreateAttributeRequest = New CreateAttributeRequest With {
 .EntityName = Contact.EntityLogicalName,
 .Attribute = New PicklistAttributeMetadata With {
  .SchemaName = "sample_examplepicklist", .LogicalName = "sample_examplepicklist",
  .DisplayName = New Label("Example Picklist", _languageCode),
  .RequiredLevel = New AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
  .OptionSet = New OptionSetMetadata With {
   .IsGlobal = True,
   .Name = _globalOptionSetName
  }
 }
}
' 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.

_serviceProxy.Execute(createRequest)

Aggiornare un set di opzioni globale

Nell'esempio seguente viene illustrato come aggiornare l'etichetta per un set di opzioni globale utilizzando 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
    }
};

_serviceProxy.Execute(updateOptionSetRequest);

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

' Use UpdateOptionSetRequest to update the basic information of an option
' set. Updating option set values requires different messages (see below).
Dim updateOptionSetRequest As UpdateOptionSetRequest = New UpdateOptionSetRequest With {
 .OptionSet = New OptionSetMetadata With {
  .DisplayName = New Label("Updated Option Set", _languageCode),
  .Name = _globalOptionSetName,
  .IsGlobal = True
 }
}

_serviceProxy.Execute(updateOptionSetRequest)

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

Opzioni degli ordini

Nell'esempio seguente viene illustrato come le opzioni in un set di opzioni globale possono essere ordinate utilizzando 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)

Recuperare tutti i set di opzioni globali

Nell'esempio seguente viene illustrato come recuperare tutti i set di opzioni globali utilizzando RetrieveAllOptionSetsRequest.


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

// Execute the request
RetrieveAllOptionSetsResponse retrieveAllOptionSetsResponse =
    (RetrieveAllOptionSetsResponse)_serviceProxy.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);
    }
}

' Use RetrieveAllOptionSetsRequest to retrieve all global option sets.
' Create the request.
Dim retrieveAllOptionSetsRequest As New RetrieveAllOptionSetsRequest()

' Execute the request
Dim retrieveAllOptionSetsResponse As RetrieveAllOptionSetsResponse =
 CType(_serviceProxy.Execute(retrieveAllOptionSetsRequest), RetrieveAllOptionSetsResponse)

' Now you can use RetrieveAllOptionSetsResponse.OptionSetMetadata property to 
' work with all retrieved option sets.
If retrieveAllOptionSetsResponse.OptionSetMetadata.Count() > 0 Then
 Console.WriteLine("All the global option sets retrieved as below:")
 Dim count As Integer = 1
 For Each optionSetMetadata As OptionSetMetadataBase In retrieveAllOptionSetsResponse.OptionSetMetadata
                       Console.WriteLine("{0} {1}",
                                         count,
                                         If(optionSetMetadata.DisplayName.LocalizedLabels.Count > 0,
                                            optionSetMetadata.DisplayName.LocalizedLabels(0).Label,
                                            String.Empty))
  count += 1
 Next optionSetMetadata
End If

Eliminare un set di opzioni globale

Nell'esempio seguente viene illustrato come controllare se un set di opzioni globale è utilizzato da un altro componente di soluzione con RetrieveDependentComponentsRequest e quindi come eliminarlo utilizzando 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)_serviceProxy.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"
};

_serviceProxy.Execute(deleteAttributeRequest);

Console.WriteLine("Referring attribute deleted.");
#endregion How to delete attribute

#region How to delete global option set

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

_serviceProxy.Execute(deleteRequest);

' Create the request to see which components have a dependency on the
' global option set.
Dim dependencyRequest As RetrieveDependentComponentsRequest =
 New RetrieveDependentComponentsRequest With {
  .ObjectId = _optionSetId,
  .ComponentType = componenttype.OptionSet
 }

Dim dependencyResponse As RetrieveDependentComponentsResponse =
 CType(_serviceProxy.Execute(dependencyRequest), RetrieveDependentComponentsResponse)

' 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.
Dim deleteAttributeRequest As DeleteAttributeRequest =
 New DeleteAttributeRequest With {
  .EntityLogicalName = Contact.EntityLogicalName,
  .LogicalName = "sample_examplepicklist"
 }

_serviceProxy.Execute(deleteAttributeRequest)

Console.WriteLine("Referring attribute deleted.")
'#End Region ' How to delete attribute

'#Region "How to delete global option set"

' Finally, delete the global option set. Attempting this before deleting
' the picklist above will result in an exception being thrown.
Dim deleteRequest As DeleteOptionSetRequest =
 New DeleteOptionSetRequest With {
  .Name = _globalOptionSetName
 }

_serviceProxy.Execute(deleteRequest)

Vedere anche

Personalizzare le applicazioni di Microsoft Dynamics 365
Utilizzare il servizio Organizzazione con i metadati di Dynamics 365
Inserire, aggiornare, eliminare e ordinare opzioni del set di opzioni globale
Messaggi del set di opzioni globale
Valori dei metadati del set di opzioni globale
Esempio: Creare un set di opzioni globale
Esempio: Utilizzare set di opzioni globali
Esempio: Eseguire il dump del set di opzioni globali in un file

Microsoft Dynamics 365

© 2017 Microsoft. Tutti i diritti sono riservati. Copyright