Programmatically add Dictionary Entries

Vote:
 

Hello,

As part of a scheduled job I want to create a new multi-value MetaField of type Dictionary items and to create the related dictionary items.  The issue is the MetaEnumItems are not being created in the mcmd_MetaEnum and mcmd_SelectedEnumValue tables.

I am missing something because they are getting persisted somewhere but I am not sure where as I can retrieve them even after a recycle of IIS.

This is what I am using to create my enum and items:

        public static bool AddEnumMetaField(string metaClassName, string name, string friendlyName, string enumName, bool isMultiValue)
        {
            var metaClass = DataContext.Current.GetMetaClass(metaClassName);
            if (metaClass != null)
            {
                if (metaClass.Fields[name] == null)
                {
                    MetaFieldType newEnum = DataContext.Current.MetaModel.RegisteredTypes.Cast().Where(type => type.McDataType == McDataType.Enum && type.Name == enumName).FirstOrDefault();

                    if (newEnum == null)
                    {
                        newEnum = MetaEnum.Create(enumName, enumName, isMultiValue);

                        newEnum.EnumItems = new Mediachase.BusinessFoundation.Data.Meta.MetaEnumItem[]
                        {
                            new Mediachase.BusinessFoundation.Data.Meta.MetaEnumItem() { AccessLevel = AccessLevel.System, Handle = 1, Name = "X1", OrderId = 1, Owner = "System"},
                            new Mediachase.BusinessFoundation.Data.Meta.MetaEnumItem() { AccessLevel = AccessLevel.System, Handle = 2, Name = "X2", OrderId = 2, Owner = "System"},
                            new Mediachase.BusinessFoundation.Data.Meta.MetaEnumItem() { AccessLevel = AccessLevel.System, Handle = 3, Name = "X3", OrderId = 3, Owner = "System"},
                            new Mediachase.BusinessFoundation.Data.Meta.MetaEnumItem() { AccessLevel = AccessLevel.System, Handle = 4, Name = "X4", OrderId = 4, Owner = "System"},
                            new Mediachase.BusinessFoundation.Data.Meta.MetaEnumItem() { AccessLevel = AccessLevel.System, Handle = 5, Name = "X5", OrderId = 5, Owner = "System"}
                        };                                               

                    }                    
                    
                    AttributeCollection attr = new AttributeCollection();
                    attr.Add(McDataTypeAttribute.EnumEditable, true);

                    metaClass.CreateMetaField(name, friendlyName, enumName, true, null, attr);                    

                    metaClass.Fields[name].AccessLevel = AccessLevel.Development;
                    metaClass.Fields[name].Owner = "Development";

                    return true;
                }
            }

            return false;
        }



        public static bool AddMetaField(MetaClass metaClass, string name, string friendlyName, string typeName)
        {            
            if (metaClass.Fields[name] == null)
            {
                metaClass.CreateMetaField(name, friendlyName, typeName, null);
                
                metaClass.Fields[name].AccessLevel = AccessLevel.Development;
                metaClass.Fields[name].Owner = "Development";                

                return true;
            }

            return false;
        }

I appreciate any help!

Note: I believe the field is being created correctly as if I manually add entries in CommerceManager then I can use them without issue.  I believe the issue is in how I am creating the enum entries.

#132623
Aug 14, 2015 1:30
Vote:
 

In situations like this - when you try to mimic a UI behavior - it helps to fire up the ole reflector =) In your case, I'd take a look in Mediachase.ConsoleManager.dll, starting with MetaFieldEditControl.SaveButton_Click and taking it from there. Good luck, and let us know your findings!

#132713
Aug 18, 2015 2:37
Vote:
 

I'm running into this problem now. Did you ever find a solution?

#145263
Mar 01, 2016 11:46
Vote:
 

Nevermind. 

When I use MetaEnum.AddItem(metaFieldType, value, orderId) instead of adding the items to the EnumItems it works.

#145272
Mar 01, 2016 13:08
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.