Share via


DeleteOptionSetRequest Class

Definition

Contains the data that is needed to delete a global choice.

public ref class DeleteOptionSetRequest sealed : Microsoft::Xrm::Sdk::OrganizationRequest
[System.Runtime.Serialization.DataContract(Namespace="http://schemas.microsoft.com/xrm/2011/Contracts")]
public sealed class DeleteOptionSetRequest : Microsoft.Xrm.Sdk.OrganizationRequest
[<System.Runtime.Serialization.DataContract(Namespace="http://schemas.microsoft.com/xrm/2011/Contracts")>]
type DeleteOptionSetRequest = class
    inherit OrganizationRequest
Public NotInheritable Class DeleteOptionSetRequest
Inherits OrganizationRequest
Inheritance
DeleteOptionSetRequest
Attributes

Examples

The following examples show how to use this message. For this sample to work correctly, you must be connected to the server to get an IOrganizationService interface instance.

Catch error if dependencies exist

You can try to delete the global choice and catch the error if it is being used by any choice column:

/// <summary>
/// Demonstrates delete of global choice with dependency error check
/// </summary>
/// <param name="service" />
static void DeleteGlobalChoiceCatchErrorExample(IOrganizationService service) {

    string choiceName = "sample_status";

    DeleteOptionSetRequest request = new() { 
          Name = choiceName
    };

    try
    {
        service.Execute(request);
    }
    catch (FaultException<OrganizationServiceFault> ex)
    {
        switch (ex.Detail.ErrorCode)
        {
            case -2147160033:
                Console.WriteLine(ex.Detail.Message);
// The OptionSet(82499bd5-6770-ee11-9ae7-000d3a9933c9) component cannot be deleted
// because it is referenced by 1 other components.
// For a list of referenced components, use the RetrieveDependenciesForDeleteRequest.

                break;
            default:
                Console.WriteLine(ex.Detail.Message);
                Console.WriteLine(ex.Detail.ErrorCode);
                break;
        }
    }
}

But this doesn't tell you which other components are using the global choice.

Get information about all dependencies

You can use the RetrieveDependenciesForDeleteRequest to determine whether the global choice has any dependencies.

/// <summary>
/// Demonstrates delete of global choice with RetrieveDependenciesForDelete check
/// </summary>
/// <param name="service" />
static void DeleteGlobalChoiceWithCheck(IOrganizationService service) {
    string choiceName = "sample_example";
    // Retrieve the global choice to get the ID
    Guid ChoiceId = RetrieveGlobalChoiceId(choiceName);
    // Get a collection of any dependencies
    EntityCollection dependentChoiceColumns = CheckDependencies(ChoiceId);

    if(dependentChoiceColumns.Entities.Any())
    {
        Console.WriteLine($"These columns are using this choice:\n");
        dependentChoiceColumns.Entities.ToList().ForEach(x => {

            string dependencyType = x.FormattedValues["dependencytype"];

            string requiredcomponenttype = x.FormattedValues["requiredcomponenttype"];
            string dependentcomponenttype = x.FormattedValues["dependentcomponenttype"];               
            Guid dependentcomponentobjectid = x.GetAttributeValue<Guid>("dependentcomponentobjectid");
            Guid dependentcomponentparentid = x.GetAttributeValue<Guid>("dependentcomponentparentid");
            Guid dependentcomponentbasesolutionid = x.GetAttributeValue<Guid>("dependentcomponentbasesolutionid");

            Console.WriteLine($"\t{dependentcomponenttype} with ID {dependentcomponentobjectid}");
            Console.WriteLine($"\tin table with ID {dependentcomponentparentid}");
            Console.WriteLine($"\tin solution with ID {dependentcomponentbasesolutionid}");
            Console.WriteLine($"\tuses this {requiredcomponenttype}");
            Console.WriteLine();
        });
    }
    else
    {
        // If there are no dependencies
        // Delete the global choice
        DeleteOptionSetRequest request = new()
        {
            Name = choiceName
        };

        service.Execute(request);
    }


    // Retrieve the global choice ID by name
    Guid RetrieveGlobalChoiceId(string choiceName) {

        RetrieveOptionSetRequest request = new() { 
              Name = choiceName,
        };

        var response = (RetrieveOptionSetResponse)service.Execute(request);

        return (Guid)response.OptionSetMetadata.MetadataId;
    }

    //Check whether there are any choice columns using this global choice.
    EntityCollection CheckDependencies(Guid choiceId) {

        RetrieveDependenciesForDeleteRequest request = new() { 
              ComponentType = 9,
              ObjectId = choiceId
        };

        var response = (RetrieveDependenciesForDeleteResponse)service.Execute(request);

        return response.EntityCollection;
    }        
}

If there is a single dependency in this example, it will output the following:

These columns are using this choice:

  Attribute with ID 523ece0d-6870-ee11-9ae7-000d3a9a098b
  in table with ID 70816501-edb9-4740-a16c-6a5efbc05d84
  in solution with ID fd140aae-4df4-11dd-bd17-0019b9312238
  uses this Option Set

Sample code on GitHub

Work with choice columns

Remarks

Usage

Pass an instance of this class to the Execute(OrganizationRequest) method, which returns an instance of DeleteOptionSetResponse.

Privileges and Access Rights

To perform this action, the user must have the System Administrator or System Customizer security role.

Notes for Callers

You cannot delete a global choice that is used by a choice column. Use RetrieveDependenciesForDeleteRequest to determine whether the global option has any dependencies.

Constructors

DeleteOptionSetRequest()

Initializes a new instance of the DeleteOptionSetRequest class

Properties

ExtensionData

Gets or sets the structure that contains extra data. Optional.

(Inherited from OrganizationRequest)
Item[String]

Gets or sets the indexer for the Parameters collection.

(Inherited from OrganizationRequest)
Name

Gets or sets the name of the global choice to delete. Required.

Parameters

Gets or sets the collection of parameters for the request. Required, but is supplied by derived classes.

(Inherited from OrganizationRequest)
RequestId

Gets or sets the ID of the request. Optional.

(Inherited from OrganizationRequest)
RequestName

Gets or sets the name of the request. Required, but is supplied by derived classes.

(Inherited from OrganizationRequest)

Applies to