Copied to clipboard

Flag this post as spam?

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


  • Reinold Beyer 11 posts 66 karma points
    Feb 25, 2011 @ 14:43
    Reinold Beyer
    0

    Get the 'Data Type' of a 'Property' in C#

    hi dear umbraco community :-)

    i'm completely new to umbraco (and after 2 weeks seem to like it really much :-), but did use dotnetnuke for small projects so far and do use sitecore professionally at my workplace...

    as i'm really bad in xslt (even sitecore does use it also extensively :-) i basically write all my stuff in c#.... a thing that perfectly woks in sitecore.

    so am i trying in umbraco (...please try not to convert me to xslt at this stage - i probably need some time :-).

    and suprisingly it also works perfectly fine in umbraco - so i'm trying to convert one custom control i wrote for sitecore to umbraco... and guess what? it worked really fast... almost :-)

    although i'm missing many pieces, i try to solve one thing now:

    the control i want to convert does basically render content - differently based on the type of property - so it renders an img-tag if i select an image via the 'media picker', or just renders the value if i have got a 'rich text editor' property, or fomats a date if the property is a 'date picker' and so on....

    so a 'one control renders any type of field' thing....

    my problem now:
    how do i get the data type a certain property uses from within the code?

    why do i need that?
    my control basically renders of course different html based on the underlying data type - and should do that automatically - so the user just throws the macro on to the template, fills the 'property alias' value and all works automatically... if only i would know what type the used property has got.

    i hope that makes sense :-)
    any reply is highly appreciated....
    thank you all

  • Tim 1193 posts 2675 karma points MVP 4x c-trib
    Feb 25, 2011 @ 15:59
    Tim
    0

    Hi Reinold,

    Here's some code that should do what you're after:

     

    //get the current page that the user is viewing
    umbraco.presentation.nodeFactory.Node node = umbraco.presentation.nodeFactory.Node.GetCurrent();

    //create a new document from the current page ID
    Document document = new Document(node.Id, true);
    string name = document.getProperty("PUT YOUR PROPERTY ALIAS VARIABLE HERE").PropertyType.DataTypeDefinition.DataType.DataTypeName;

    WARNING: This might not be super fast, as the Document API is quite slow (as it makes numerous database calls). I'm not sure how else you'd get the data type name though! If you had a lot of this macro on a page, it might run quite slowly and not scale well. If it becomes a problem, you could maybe look at writing a sql query to look up the information in one trip, or you could look at caching the results, or maybe consider using a naming convention for the property aliases that contains the datatype.

    Hope that helps!

    :)

     

  • Reinold Beyer 11 posts 66 karma points
    Feb 26, 2011 @ 12:46
    Reinold Beyer
    0

    thank you very much tim!

    i thought that this can't be solved easily (although that might be a good enhancement in future releases... having the data type in a property for a 'node property' might be helpful in different scenarious :-)

    as i want to have this control as the major control that renders actually all (normal) content on a site, it is not perfect that it does so many db calls (as you say...)

    making my own direct query might work, but i have no idea at the moment how that works... will have to investigate :-)

    the naming convention idea is actually a very good one - although it will be a problem if someone else wants to use the control in a different environment (if i give it away as a package...)

    what i tried was also telling the control what data type it has to render - i wanted to use a public enum within the control for all 'render able data types', but i have a problem getting the enum values to be selectable on the macro properties - is there a possibility for that?
    or is it possible to create a string property on a macro that gets the data types from a drop down? some other values are possible - but not the data types :-/ or can i have a custom drop down, with custom values for macro properties?

    anyway, great response! thank you very much! i will find my way!

  • Tim 1193 posts 2675 karma points MVP 4x c-trib
    Feb 28, 2011 @ 11:00
    Tim
    0

    I'm not sure about getting a selectable enum for the Macro, there isn't a way to do it out of the box, macros only support a few basic items out of the box for properties that you can pass in. You should be able to code a custom macro parameter type though, although I'm not sure how you'd go about it. I did find this old post that might help though: http://forum.umbraco.org/yaf_postst9402_dropdown-as-macro-parameter.aspx

    Hope that helps!

    :)

Please Sign in or register to post replies

Write your reply to:

Draft