Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

How to add an option to Multiselect option set porgramtically?

(0) ShareShare
ReportReport
Posted on by

Hello,

I found in MS docs how to create with c# a Multiselect option set as shown in the code below :

    int _languageCode = 1033; //English

    MultiSelectPicklistAttributeMetadata outDoorActivitiesAttribute = new MultiSelectPicklistAttributeMetadata()
    {
    SchemaName = "sample_OutdoorActivities",
    LogicalName = "sample_outdooractivities",
    DisplayName = new Label("Outdoor activities", _languageCode),
    RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
    Description = new Label("Outdoor activities that the contact likes.", _languageCode),
    OptionSet = new OptionSetMetadata()
        {
            IsGlobal = false,
            OptionSetType = OptionSetType.Picklist,
            Options = {
                new OptionMetadata(new Label("Swimming",_languageCode),1),
                new OptionMetadata(new Label("Hiking",_languageCode),2),
                new OptionMetadata(new Label("Mountain Climbing",_languageCode),3),
                new OptionMetadata(new Label("Fishing",_languageCode),4),
                new OptionMetadata(new Label("Hunting",_languageCode),5),
                new OptionMetadata(new Label("Running",_languageCode),6),
                new OptionMetadata(new Label("Boating",_languageCode),7),
                new OptionMetadata(new Label("Skiing",_languageCode),8),
                new OptionMetadata(new Label("Camping",_languageCode),9)}
        }
    };

    CreateAttributeRequest createAttributeRequest = new CreateAttributeRequest
    {
        EntityName = Contact.EntityLogicalName,
        Attribute = outDoorActivitiesAttribute
    };

But my need is how to add a new OptionMetadata with a new label and new value to an existant Multiselect option set ?

  • Community Member Profile Picture
    on at
    RE: How to add an option to Multiselect option set porgramtically?

    Thanks Mehdi you are the best

  • Community Member Profile Picture
    on at
    RE: How to add an option to Multiselect option set porgramtically?

    Hi Mehdi,

    Yes you are right it work for both insertOptionValueRequest and updateOptionValueRequest.

  • meelamri Profile Picture
    13,212 User Group Leader on at
    RE: How to add an option to Multiselect option set porgramtically?

    It worked in my environment for the multiselect option set.

  • Community Member Profile Picture
    on at
    RE: How to add an option to Multiselect option set porgramtically?

    Hi Mehdi,

    Thank you for your quick answer, but i believe it will work just for an optionset not for a multiselect optionset.

  • Verified answer
    meelamri Profile Picture
    13,212 User Group Leader on at
    RE: How to add an option to Multiselect option set porgramtically?

    Hi, 

    Please refer to the code below snippet from MS Docs:

    Local option:

    // Create a request.
    InsertOptionValueRequest insertOptionValueRequest =
        new InsertOptionValueRequest
    {
        AttributeLogicalName = "new_picklist",
        EntityLogicalName = Contact.EntityLogicalName,
        Label = new Label("New Picklist Label", _languageCode)
    };
    
    // Execute the request.
    int insertOptionValue = ((InsertOptionValueResponse)svc.Execute(
        insertOptionValueRequest)).NewOptionValue;
    
    Console.WriteLine("Created {0} with the value of {1}.",
        insertOptionValueRequest.Label.LocalizedLabels[0].Label,
        insertOptionValue);

    Global option:

    // Use InsertOptionValueRequest to insert a new option into a 
    // global option set.
    InsertOptionValueRequest insertOptionValueRequest =
        new InsertOptionValueRequest
        {
            OptionSetName = _globalOptionSetName,
            Label = new Label("New Picklist Label", _languageCode)
        };
    
    // Execute the request and store the newly inserted option value 
    // for cleanup, used in the later part of this sample.
    _insertedOptionValue = ((InsertOptionValueResponse)svc.Execute(
        insertOptionValueRequest)).NewOptionValue;
    
    //Publish the OptionSet
    PublishXmlRequest pxReq2 = new PublishXmlRequest { ParameterXml = String.Format("{0}", _globalOptionSetName) };
    svc.Execute(pxReq2);

    Based on the documentation, this will work for the option set. However, it also works for Multiselect option set too. (tested with global)
    https://docs.microsoft.com/en-us/dotnet/api/microsoft.xrm.sdk.messages.insertoptionvaluerequest?view=dynamics-general-ce-9

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,000 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,850 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Product updates

Dynamics 365 release plans