Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tom 713 posts 954 karma points
    Mar 22, 2017 @ 03:09
    Tom
    1

    Programatically add a drop down list data type and prevalues?

    Hi I was wondering how to add a new drop down list data type and prevalues to a document type programatically..

    We used to use something like so: var ddl = new DataTypeDefinition(-1, controlGuid); ddl.DatabaseType = DataTypeDatabaseType.Ntext; ddl.Key = Guid.NewGuid(); ddl.Name = name;

            ServiceContext.DataTypeService.Save(ddl);
    
            foreach (var value in preValues)
            {
                var p = new umbraco.cms.businesslogic.datatype.PreValue(0, 0, value);
                p.DataTypeId = ddl.Id;
                p.Save();
            }
    
            return ddl;
    

    But this isn't working in the latest version of umbraco and datatypedefinitions are now slated to be deprecated?

    Thanks

  • Rob Watkins 369 posts 701 karma points
    Jan 10, 2018 @ 10:18
    Rob Watkins
    0

    I'm sure you've fixed this now, but for current readers, I wrote a helper function to merge prevalues in bulk so I could provide lookup editing for a client on drop down lists. If you needed replace/delete functionality then it'd be easy to add, I just haven't done it yet :)

    This uses all current, non-deprecated functions.

    Requires:

    using UC = Umbraco.Core;

    Code:

            /// <summary>
        /// Merges new prevalues to an existing datatype.
        /// For dictionary based prevalues, you must provide a dictionary key to merge with. For simple DropdownLists etc this is the same as the label
        /// For array based prevalues, it merges based on the actual value, NOT the id
        /// </summary>
        public static MergePreValueResult MergePreValues(UC.Models.PropertyType propertytype, List<string> arrayvalues = null, IDictionary<string, string> dictionaryvalues = null)
        {
            MergePreValueResult res = new MergePreValueResult();
            UC.Services.IDataTypeService datatypeservice = UC.ApplicationContext.Current.Services.DataTypeService;
    
            UC.Models.IDataTypeDefinition datatypedef = datatypeservice.GetDataTypeDefinitionById(propertytype.DataTypeDefinitionId);
    
            if (datatypedef != null)
            {
                Dictionary<string, UC.Models.PreValue> final = new Dictionary<string, UC.Models.PreValue>();
                UC.Models.PreValueCollection currentprevalues = datatypeservice.GetPreValuesCollectionByDataTypeId(propertytype.DataTypeDefinitionId);
    
                if (currentprevalues != null)
                {
                    res.Success = true;
                    UC.Models.PreValue prevalue = null;
    
                    res.IsDictionary = currentprevalues.IsDictionaryBased;
    
                    if (currentprevalues.IsDictionaryBased && dictionaryvalues != null)
                    {
                        foreach (KeyValuePair<string, string> kp in dictionaryvalues.Union(currentprevalues.PreValuesAsDictionary.ToDictionary(xk => xk.Key, xv => xv.Value.Value)))
                        {
                            if (currentprevalues.PreValuesAsDictionary.ContainsKey(kp.Key))
                            {
                                prevalue = currentprevalues.PreValuesAsDictionary[kp.Key];
    
                                prevalue.Value = kp.Value;
                                res.Updated++;
                            }
                            else
                            {
                                prevalue = new UC.Models.PreValue(kp.Value);
                                res.Created++;
                            }
    
                            final.Add(kp.Key, prevalue);
                        }
                    }
                    else if (!currentprevalues.IsDictionaryBased && arrayvalues != null)
                    {
                        foreach (string value in arrayvalues.Union(currentprevalues.PreValuesAsArray.Select(x => x.Value)))
                        {
                            prevalue = currentprevalues.PreValuesAsArray.FirstOrDefault(x => x.Value == value);
    
                            if (prevalue != null)
                            {
                                prevalue.Value = value;
                                res.Updated++;
                            }
                            else
                            {
                                prevalue = new UC.Models.PreValue(value);
                                res.Created++;
                            }
    
                            final.Add(null, prevalue);
                        }
                    }
                }
    
                datatypeservice.SaveDataTypeAndPreValues(datatypedef, final);
            }
    
            return res;
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft