Copied to clipboard

Flag this post as spam?

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


  • Cedric 15 posts 86 karma points
    Jul 26, 2018 @ 11:09
    Cedric
    0

    Retrieve Name of Custom Property

    Hi all, Please how to retrieve the Name of a Custom property ? For example :

    var selection = Umbraco.TypedContent(1158).Children("regulatory")
            .Where(x => x.IsVisible())
            .Where(x => x.Name == "France");
                    foreach (var item in selection)
                    {
                        item.GetPropertyValue("bandName").ToString();
                        //I want the Name of bandName ????
                    }
    
  • Paul Tilsed 26 posts 179 karma points c-trib
    Jul 26, 2018 @ 14:29
    Paul Tilsed
    0

    Hi Cedric,

    By Name I'm guessing you want to get the value of the property with the alias of "bandName"?

    Assuming the property editor you have used is a simple textbox then the code you already have will be able to get the value out. A better way to do it might be with this line of code:

      var bandName =  item.GetPropertyValue<string>("bandName");
    

    Remember to check the alias is correct and your query is pulling back results in the first place.

    If you have a more complex property editor then you might need to use a PropertyValueConverter to pull the object out and then select the appropriate value from there.

    Paul

  • Cedric 15 posts 86 karma points
    Jul 26, 2018 @ 15:38
    Cedric
    0

    Hi Paul, Thanks for the answer but for the value I have no problem I try to retrieve the name of the textbox field corresponding to my alias for example for my alias BandNumber are name is Band Number for example for my Tel alias are name is Tel Number I hope you understand my explanation Cédric

  • Paul Tilsed 26 posts 179 karma points c-trib
    Jul 26, 2018 @ 15:53
    Paul Tilsed
    0

    Hi Cedric,

    I think I can see what you are trying to do now but I'm unsure why you would need to take this approach. The property name is not really part of the content, for that reason, it's not included in the IPublishedContent model and is not easily accessible without expensive queries against the ContentTypeService.

    I would recommend you hardcode the values, you are hardcoding the alias so why not the name?

    If you really do need to get the name for a property on a content type then as I said the ContentTypeService is where you will find that information.

    Something like this might work depending on your content type make up.

    var propertyName = Services.ContentTypeService.GetContentType("ContentTypeID").PropertyGroups.FirstOrDefault(x => x.Name == "PropertyGroup(Tab)Name").PropertyTypes.FirstOrDefault(x => x.Alias == "bandName").Name;
    

    Hope this helps,

    Paul

  • Cedric 15 posts 86 karma points
    Jul 26, 2018 @ 16:08
    Cedric
    0

    Thank you, I need this function because the user backoffice has the opportunity to rename the name of the textbox and it avoids me to change it in the code. After I hardcoder the fields because I do not see how to create my model view with my controller relative to a type of document. thanks for the help

Please Sign in or register to post replies

Write your reply to:

Draft