Copied to clipboard

Flag this post as spam?

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


  • Jeppe 2 posts 22 karma points
    Mar 31, 2015 @ 15:40
    Jeppe
    0

    Getting the name of a Data type through .Properties?

    Hello all!

    This is my first post, so be gentle :).

    I have been looking for a solution for quite a while now. I've been trying loop through all the properties on a page and create some HTML based on the generic properties that are added to the document template.

     @foreach (var prop in Umbraco.TypedContent(1241).FirstChild().Properties)
            {
                @prop.PropertyTypeAlias<br />
                @umbraco.library.GetPropertyTypeName("Medlem", prop.PropertyTypeAlias)<br /><br />
            }
    

    This returns both the name and alias. But i also want to get the data type name of the property, so that i can make an if statement (if the name of the property is == "Medlems bool", which is basically a true/false with a different name) - so i can get rid of properties i dont want to loop through.

    Is what i am trying to do possible, does Umbraco even expose the data type names?

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Mar 31, 2015 @ 17:30
    Jeroen Breuer
    0

    Hello,

    To get the datatype name you probably need to use an API that does db calls and I'm not if that's what you want. Can't you just check on the property alias and based on that skip it if you want?

    Jeroen

  • Jeppe 2 posts 22 karma points
    Mar 31, 2015 @ 17:48
    Jeppe
    0

    I have also considered doing that. But the thing is at the moment i already have about 10 bools, and most likely more coming, it would have been a lot smoother if i could just check on the name :(

    It doesnt help that they are placed in the same tab, right?

    Do you by any chance have another suggestion / solution, it has to be easy to edit for the customer at the end :)

  • Nicholas Westby 2054 posts 7103 karma points c-trib
    Mar 31, 2015 @ 17:50
    Nicholas Westby
    0

    This will do it:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        // ... other code omitted...
        var ctService = ApplicationContext.Current.Services.ContentTypeService;
        var dtService = ApplicationContext.Current.Services.DataTypeService;
        var myType = ctService.GetContentType(Model.Content.DocumentTypeAlias);
        var properties = myType.PropertyTypes.Select(x => new
        {
            x.Alias,
            Type = dtService.GetDataTypeDefinitionById(x.DataTypeDefinitionId).Name
        });
    }
    
    @foreach (var p in properties)
    {
        <div>
            @p.Alias (@p.Type)
        </div>
    }
    

    Like Jeroen says, this will be very, very slow. You should do some caching in static variables (with appropriate locking, of course) to avoid performance issues.

Please Sign in or register to post replies

Write your reply to:

Draft