Copied to clipboard

Flag this post as spam?

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


  • Thomas 43 posts 123 karma points
    Jul 27, 2018 @ 13:14
    Thomas
    0

    How to find the umbraco property data type?

    I want to identify which property is used for every properties? example in umbraco page i have added four properties with suitable controls (like text box check boxes, optional, text area). so here when i retrieve all umbraco properties in web page i want to know each property has which type

    Is that possible any properties is available to differentiate instead of UmbracoDefinitionDatatypeId ?

  • Søren Gregersen 441 posts 1884 karma points MVP 2x c-trib
    Jul 27, 2018 @ 14:08
    Søren Gregersen
    0

    Hi Thomas,

    If I understand the question correctly, you should be able to use the GetProperty([alias]) method on content to get information about a property.

    I hope this helps.

  • Thomas 43 posts 123 karma points
    Jul 31, 2018 @ 13:55
    Thomas
    0

    Thanks for the update. Actually i have N number of properties in a content and iam trying to access one by one. So in this i want to check each property data type.

    How can i get each property data type?

  • Søren Gregersen 441 posts 1884 karma points MVP 2x c-trib
    Jul 31, 2018 @ 14:26
    Søren Gregersen
    0

    If you are using modelsbuilder you could use reflection to get all the properties.

    Another way is to look as the ContentType which exposes a PropertyTypes-collection.

    You should not use the ApplicationContext.Services (ContentService etc) for this, since that will do lookups in the database.

  • Garðar Þorsteinsson 119 posts 565 karma points
    Jul 31, 2018 @ 14:34
    Garðar Þorsteinsson
    0

    You are right about the ContentType.

    Should be able to access all the data like this.

        var propTypes  = Model.Content.ContentType.PropertyTypes;
    
        foreach (var type in propTypes)
        {
            var editorAlias = type.PropertyEditorAlias;
            var typeAlias = type.PropertyTypeAlias;
            var value = Model.Content.GetPropertyValue(typeAlias);
        }
    
  • Garðar Þorsteinsson 119 posts 565 karma points
    Jul 31, 2018 @ 14:10
    Garðar Þorsteinsson
    0

    Hi Thomas,

    I do not think this is possible in a good way.

    The property type is not saved in the cache so you would have to use the content service, but that of course talks straight to the sql and would never recommend using it in a view but you could read the data and store it in a cache that you could use later in a view.

    Here is an example how you could use the content service to fetch the data in a view.

    var cs = ApplicationContext.Current.Services.ContentService;
    
    var content = cs.GetById(Model.Content.Id);
    
    var properties = content.Properties;
    
    foreach (var p in properties)
    {
        var type = p.PropertyType;
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft