Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 79901

How to copy Option Set Attribute with Global Option Set via the SDK with C#

$
0
0

Hello Experts,

I am trying to duplicate all fields of an entity into another entity via a C# ConsoleApp.

I am struggling to duplicate an attribute of type Picklist (Option Set in CRM) which has an underlying Global Option Set.

I am getting 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."

This is the code:

*NOTE: This code works for Attributes of type Picklist (Option Set in CRM) which are NOT Global. It only fails when it encounters an attribute which uses a Global Option Set.

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

Thanks,

Victor


Viewing all articles
Browse latest Browse all 79901

Trending Articles