Copied to clipboard

Flag this post as spam?

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


  • Casper 70 posts 308 karma points
    Aug 19, 2016 @ 12:19
    Casper
    1

    IPublishedContent property datatype

    Hi,

    Is it possible to get an IPublishedContent property to tell me what datatype it has assigned for itself?

    Ps. I did try to ask it but no answer.

    Thanks,

    //casper

  • Kevin Jump 2343 posts 14890 karma points MVP 8x c-trib
    Aug 20, 2016 @ 10:07
    Kevin Jump
    101

    Hi Casper

    the short answer is not easily, and the second answer is I would try not to if i was you :)

    but the third answer ....

    IPublishedContent is getting your content from the cache and this is loosly coupled from the data, so while you can tell if it's say and INT or a string, its much harder to work out if it's something more special. (you can cast properties types and use the Core Property Value converter package but you do have to know what they are)

    if you really need to know what the underling datatype is then you need to go to the back office api which will hit the database and will be slow!!

    One way:

    1. get the doctype from the published item,
    2. get the property from the doctype
    3. get the datatype from the property

    .

    var docTypeAlias = Model.Content.DocumentTypeAlias;
    
    var docType = ApplicationContext.Current.Services.ContentTypeService.GetContentType(docTypeAlias);
    
    var property = docType.PropertyTypes.Where(x => x.Alias == "propertyAlias").FirstOrDefault();
    if (property != null)
    {
        var datatype = ApplicationContext.Current.Services.DataTypeService
            .GetDataTypeDefinitionById(property.DataTypeDefinitionId);
    
        var name = datatype.Name;
    }
    

    Second way,

    1. get the Content item from the published content
    2. get the property from the content
    3. use that to get the data type

    .

    var content = ApplicationContext.Current.Services.ContentService.GetById(Model.Content.Id);
    if (content.HasProperty("propertyName"))
    {
        var property = content.Properties.Where(x => x.Alias == "propertyAlias").FirstOrDefault();
        if (property != null)
        {
            ....
        }
    
    }
    

    but neither of these is really right, they will be slow, and hit the database a bit.

    if it's certain type you are looking for you might just be better sniffing for it, based on the structure so if you look in the app_data/umbraco.config file (which is the cache, don't lock it or edit it!). you might see that the type you are looking for is JSON or has a certain format you can just detect in the string.

  • Casper 70 posts 308 karma points
    Aug 22, 2016 @ 16:22
    Casper
    1

    Hi Kevin,

    Great answers - They got me to the right place and stuff is working :)

    My need was to duplicate "Umbraco.DropDown" on an angular front-end. For anybody interested, this is how i got there:

    public class UmbracoDropDown
    {
        public int Id { get; set; }
        public string Alias { get; set; }
        public string Value { get; set; }
        public List<string> Options { get; set; } = new List<string>();
    }
    
    public UmbracoDropDown GetDropDown(int contentId, string propertyName)
    {
        var content = _contentService.GetById(contentId);
        var dropDown = content?.Properties.First(x => x.PropertyType.PropertyEditorAlias == "Umbraco.DropDown" && x.Alias.InvariantEquals(propertyName));
        if (dropDown == null)
            return null;
        var model = new UmbracoDropDown()
        {
            Id = dropDown.Id,
            Alias = dropDown.Alias,
            Value = _dataTypeService.GetPreValueAsString(content.GetValue<int>(dropDown.PropertyType.Alias)), 
            Options = _dataTypeService.GetPreValuesByDataTypeId(dropDown.PropertyType.DataTypeDefinitionId).ToList()
        };
        return model;        
    }
    

    The end result being something like this:

    {
        "Id": 41428,
        "Alias": "heatProviderOptions",
        "Value": "Inklusiv",
        "Options": [
            "Tilmelding",
            "Inklusiv",
            "Aconto"
        ]
    }
    

    Thanks man!

Please Sign in or register to post replies

Write your reply to:

Draft