Copied to clipboard

Flag this post as spam?

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


  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Jun 08, 2020 @ 15:12
    Ismail Mayat
    0

    Getting prevalues from datatype

    I have a datatype of type dropdown and it has prevalues set. I have a content publishing event where i need to get these prevalues and test against values entered for item being published.

    Initially I had

     private readonly IDictionary<string, PreValue> _audiences;
        public Content()
        {
            var audiencesDatatype = ApplicationContext.Current.Services.DataTypeService.GetPreValuesCollectionByDataTypeId(1073);
    
            _audiences = audiencesDatatype.PreValuesAsDictionary;
    
        }
    

    Which works but when on different envs it does not as the id of the datatype will be different.

    The data is called audience its of type DropDown.

    So what is the best way of getting those prevalues but using the alias or name of the data type? I tried:

    var property = ApplicationContext.Current.Services.DataTypeService
                .GetDataTypeDefinitionByPropertyEditorAlias("audience").First();
    
    
            var audiencesDatatype =
                ApplicationContext.Current.Services.DataTypeService.GetPreValuesCollectionByDataTypeId(property.Id);
    
            _audiences = audiencesDatatype.PreValuesAsDictionary;
    

    but the call to get the property editor is always returning null. So is this the right way to do it?

    Regards

    Ismail

  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Jun 08, 2020 @ 15:41
    Ismail Mayat
    0

    So I have gone for:

    public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            Init();
    
            ContentService.Saving += Content_Saving;
    
            ContentService.Publishing += Content_Publishing;
    
        }
    
        private void Init()
        {
            var all = ApplicationContext.Current.Services.DataTypeService.GetAllDataTypeDefinitions();
    
            //this property name in data types must not change, also if any values in it are changed we need code rebuild or site restart 
            //to clear cache
            var property = all.First(x => x.Name == "Alert Audience");
    
            var audiencesDataType =
                ApplicationContext.Current.Services.DataTypeService.GetPreValuesCollectionByDataTypeId(property.Id);
    
            _audiences = audiencesDataType.PreValuesAsDictionary;
        }
    

    To be honest this property and its data will not change as its already in use and is critical to rest of site so will go with this.

  • Kevin Jump 2310 posts 14695 karma points MVP 7x c-trib
    Jun 08, 2020 @ 16:16
    Kevin Jump
    100

    OK, so i couldn't resist really.

    reason the first method was failing is that you need to pass the datatype id to GetPreValuesCollectionByDataTypeId not the property Id.

    so . you have to call with the definitionId not the property one

    dataTypeService.GetPreValuesCollectionByDataTypeId(propertyType.DataTypeDefinitionId)
    

    but if you want to have something that looks up the value only when it needs to, but also copes with the list values changing

       public class DropDownListChecker : ApplicationEventHandler
        {
            private IDataTypeService dataTypeService;
            private IRuntimeCacheProvider cacheProvider;
    
            private string listProperty;
            private string listPropertyType; // could just use this - see note.
    
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                listProperty = "alertAudience";
                listPropertyType = "audienceList";
    
                dataTypeService = applicationContext.Services.DataTypeService;
                cacheProvider = applicationContext.ApplicationCache.RuntimeCache;
    
                ContentService.Publishing += ContentService_Publishing;
                DataTypeService.Saved += DataTypeService_Saved;
    
            }
    
            private void ContentService_Publishing(Umbraco.Core.Publishing.IPublishingStrategy sender, Umbraco.Core.Events.PublishEventArgs<Umbraco.Core.Models.IContent> e)
            {
                foreach(var item in e.PublishedEntities)
                {
                    // you could do 
                    //   if (item.PropertyTypes.Any(x => x.Alias.InvariantEquals(listPropertyType)))
                    // but it might be slower (would need to test). 
                    if (item.HasProperty(listProperty))
                    {
                        var property = item.Properties.FirstOrDefault(x => x.Alias.InvariantEquals(listProperty));
                        if (property != null)
                        {
                            var values = GetPropertyPreValues(property.PropertyType);
                        }
    
                    }
                }
            }
    
            private IDictionary<string, PreValue> GetPropertyPreValues(PropertyType propertyType)
            {
                // Get the values from the cache, if they are not there look them up via the service (db)
                return cacheProvider.GetCacheItem<IDictionary<string, PreValue>>($"prevalue_{propertyType.Alias}",
                    () =>
                    {
                        var collection = dataTypeService.GetPreValuesCollectionByDataTypeId(propertyType.DataTypeDefinitionId);
                        return collection.PreValuesAsDictionary;
                    });
            }
    
            /// <summary>
            ///  if the data type is saved, clear the cache, because the list might have changed
            /// </summary>
            private void DataTypeService_Saved(IDataTypeService sender, Umbraco.Core.Events.SaveEventArgs<IDataTypeDefinition> e)
            {
                if (e.SavedEntities.Any(x => x.Name.InvariantEquals(listPropertyType)))
                {
                    cacheProvider.ClearCacheByKeySearch("prevalue_");
                }
            }
    
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft