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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

How to Programmatically Create Option Set Attribute with underlying Global Option Set via CRM SDK

(0) ShareShare
ReportReport
Posted on by

Hello Experts,

I am trying to create an Option Set in an entity programmatically via the CRM SDK.

The code below works for Option Sets which are not Global, but when it encounters an Attribute with underlying Global Option Set it throws this Error:

"Error: You cannot create a system option set directly through the SDK. Only custom option sets can be created directly through the SDK."

OR

"Error: You cannot set Options property for an Attribute that links to an existing Global OptionSet."

My Code:

foreach (AttributeMetadata attr in attributes)
{
    /* Will process Attributes which: */
    if (attr.IsCustomAttribute != null && attr.IsCustomAttribute.Value == true &&             /* Are Custom Attributes (Created by User) */
        attr.AttributeType.HasValue && attr.AttributeType.Value.ToString() == "Picklist" &&   /* AND are of Type Option Set */
        !logicalNamesToIgnore.Contains(attr.LogicalName))                                     /* AND are NOT in the IGNORE list */
    {
        EnumAttributeMetadata enumAttr = (EnumAttributeMetadata)attr;

        CreateAttributeRequest createAttributeRequest = new CreateAttributeRequest
        {
            EntityName = ChildEntityLogicalName,
            Attribute = enumAttr
        };

        service.Execute(createAttributeRequest);

        Console.WriteLine($"Attribute Created ---> {enumAttr.LogicalName}");
    }
}

 

Do you have any suggestion on how to create it? Do I need to create a RelationshipRequest ? Any good articles help!

Thanks!

My Solution:

foreach (AttributeMetadata attr in attributes)
{
    /* Will process Attributes which: */
    if (attr.IsCustomAttribute != null && attr.IsCustomAttribute.Value == true &&           /* Are Custom Attributes (Created by User) */
        attr.AttributeType.HasValue && attr.AttributeType.Value.ToString() == "Picklist")   /* AND are of Type Option Set */
    {
        EnumAttributeMetadata enumAttr = (EnumAttributeMetadata)attr;
        enumAttr.MetadataId = new Guid();    /* This is to avoid GUID collision */

        if (enumAttr.OptionSet.IsGlobal == false) /* If it is NOT Global Option Set */
        {
            enumAttr.OptionSet.MetadataId = new Guid();   /* This is to avoid GUID collision */
            enumAttr.OptionSet.Name = ChildEntityLogicalName   enumAttr.OptionSet.Name.Substring(ParentEntityLogicalName.Length); /* Replace Parent LogicalName with Child LogicalName */
        }
        else
        {
            Console.WriteLine("Global Option Set Name ---> "   enumAttr.OptionSet.Name);
            enumAttr.OptionSet.Options.Clear();
        }

        CreateAttributeRequest createAttributeRequest = new CreateAttributeRequest
        {
            EntityName = ChildEntityLogicalName,
            Attribute = enumAttr
        };

        service.Execute(createAttributeRequest);

        Console.WriteLine($"Attribute Created ---> {enumAttr.LogicalName}");
    }
}

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

    Hi,

    Maybe this can help you: richcrm.wordpress.com/.../

  • Community Member Profile Picture
    on at

    Hello,

    I managed to programatically add an Option Set (Picklist) Attribute via the SDK, by setting its OptionSet.Options to null (Clear())

    This is the code which I used (Will also post it in Question):

    foreach (AttributeMetadata attr in attributes)

               {

                   /* Will process Attributes which: */

                   if (attr.IsCustomAttribute != null && attr.IsCustomAttribute.Value == true &&            /* Are Custom Attributes (Created by User) */

                       attr.AttributeType.HasValue && attr.AttributeType.Value.ToString() == "Picklist" &&   /* AND are of Type Option Set */

                       !logicalNamesToIgnore.Contains(attr.LogicalName))                                                     /* AND are NOT in the IGNORE list */

                   {

                       EnumAttributeMetadata enumAttr = (EnumAttributeMetadata)attr;

                       enumAttr.MetadataId = new Guid();    /* This is to avoid GUID collision */

                       if (enumAttr.OptionSet.IsGlobal == false) /* If it is NOT Global Option Set */

                       {

                           enumAttr.OptionSet.MetadataId = new Guid();   /* This is to avoid GUID collision */

                           enumAttr.OptionSet.Name = ChildEntityLogicalName + enumAttr.OptionSet.Name.Substring(ParentEntityLogicalName.Length); /* Replace Parent LogicalName with Child LogicalName */

                       }

                       else

                       {

                           Console.WriteLine("Global Option Set Name ---> " + enumAttr.OptionSet.Name);

                           enumAttr.OptionSet.Options.Clear();

                       }

                       CreateAttributeRequest createAttributeRequest = new CreateAttributeRequest

                       {

                           EntityName = ChildEntityLogicalName,

                           Attribute = enumAttr

                       };

                       service.Execute(createAttributeRequest);

                       Console.WriteLine($"Attribute Created ---> {enumAttr.LogicalName}");

                   }

               }

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Hamza H Profile Picture

Hamza H 140 Super User 2026 Season 1

#2
Nagaraju_Matta Profile Picture

Nagaraju_Matta 128

#3
Abhilash Warrier Profile Picture

Abhilash Warrier 70 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans