Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Suggested answer

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

Posted on by Microsoft Employee

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}");
    }
}

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to Programmatically Create Option Set Attribute with underlying Global Option Set via CRM SDK

    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}");

                   }

               }

  • Suggested answer
    meelamri Profile Picture
    meelamri 13,198 User Group Leader on at
    RE: How to Programmatically Create Option Set Attribute with underlying Global Option Set via CRM SDK

    Hi,

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

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,537 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,520 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans