Copied to clipboard

Flag this post as spam?

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


  • Sonja 133 posts 621 karma points
    Oct 16, 2019 @ 10:57
    Sonja
    0

    Get property value of a field

    Hi,

    I have property name of a field and I need to get the value. For example I have "Device Name" and the alias is deviceName. So how do I get the value of this field if I have only "Device Name":

    Thanks

  • Kevin Jump 2348 posts 14896 karma points MVP 8x c-trib
    Oct 16, 2019 @ 11:40
    Kevin Jump
    0

    Hi Sonja,

    you can't guarantee it will always work because the user can choose to manually change the alias of a property, but :

    the .ToSafeAlias() string extension would do this for you.

    e.g

    var propertyName = "Device Name";
    var propertyAlias = propertyName.ToSafeAlias(); 
    

    requires Using Umbraco.Core

  • Sonja 133 posts 621 karma points
    Oct 16, 2019 @ 11:45
    Sonja
    0

    Hi Kevin, I don't need this, the name can be "Device Name" but the property alias can be devName I don't think it will work like this, and I need it to work always

  • Steve Megson 151 posts 1024 karma points MVP c-trib
    Oct 16, 2019 @ 12:20
    Steve Megson
    0

    The cached published content only knows the aliases, so I think you'd have to use the ContentTypeService. You'd probably want to cache the results if you need to do this a lot.

    Something like this should work:

    var contentTypeId = publishedContent.ContentType.Id;
    var contentType = ApplicationContext.Services.ContentTypeService.GetById(contentTypeId);
    var property = contentType.CompositionPropertyTypes.FirstOrDefault(x => x.Name == propertyName);
    var propertyAlias = property.Alias;
    
  • Sonja 133 posts 621 karma points
    Oct 18, 2019 @ 05:57
    Sonja
    0

    Hi Steve,

    can I do this in a partial view? Also I need the value, not the alias.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies