web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

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 ?

I have the same question (0)
  • Verified answer
    meelamri Profile Picture
    13,216 User Group Leader on at

    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

  • Community Member Profile Picture
    on at

    Hi Mehdi,

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

  • meelamri Profile Picture
    13,216 User Group Leader on at

    It worked in my environment for the multiselect option set.

  • Community Member Profile Picture
    on at

    Hi Mehdi,

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

  • Community Member Profile Picture
    on at

    Thanks Mehdi you are the best

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Martin Dráb Profile Picture

Martin Dráb 41 Most Valuable Professional

#2
iampranjal Profile Picture

iampranjal 39

#3
Satyam Prakash Profile Picture

Satyam Prakash 35

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans