Copied to clipboard

Flag this post as spam?

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


  • Dave de Moel 122 posts 574 karma points c-trib
    Aug 28, 2015 @ 18:19
    Dave de Moel
    0

    Getting al properties from a certain tab

    Hey guys I have a question

    For a website I have multiple pages with similar, but not all equal, properties. What I did now, was make a composite type with all common properties, and have made each document type for the other pages inherit the composite type, and extend on the tab defined in the composite type.

    What I know want to do, is loop over all properties of that specific tab. How can I do that?

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Aug 30, 2015 @ 10:53
    Marc Goodson
    0

    Hi Dave

    I'm not sure if you are trying to do this on the front end of your website, or in the back office, and I'm not sure why you need to loop over the properties; if you could give us some context there may be a better approach.

    With Umbraco you have to be slightly aware of when you are accessing the published content xml file and when you are accessing the database directly...

    so accessing the published xml cache file, allows you to loop over the properties of a particular IPublishedContent item:

    @foreach (var prop in Model.Content.Properties)
    {
        <li>@prop.PropertyTypeAlias</li>
    }
    

    But not 'by tab' (or PropertyGroup)

    However if you use the Umbraco Services API's which do hit the Umbraco database, and so therefore are slow, these services are not optimised for read only access, but for CRUD data manipulation, you could use:

     var cts = ApplicationContext.Current.Services.ContentTypeService;
                var contentType = cts.GetContentType("yourContentTypeAlias");
                foreach (var propGroup in contentType.PropertyGroups)
                {
                    @propGroup.Name
                    if (propGroup.Name == "yourTabName")
                    {
                        foreach (var propertyType in propGroup.PropertyTypes)
                        {
                            var propertyAlias = propertyType.Alias;
                            <li>@propertyAlias</li>
                        }
                    }
                }   
    

    To use the ContentTypeService to get a reference to the content type, a contenttype then has a collection of 'PropertyGroups' (eg tabs) and each PropertyGroup has a collection of PropertyTypes etc

    So this would be ok to use to manipulate the backoffice, but really slow on the front end of your site - but understanding more what you are trying to achieve, would probably yield the suggestion of a different approach.

    but this more answers your question, rather than says it is a good idea!

  • Dave de Moel 122 posts 574 karma points c-trib
    Aug 30, 2015 @ 12:11
    Dave de Moel
    100

    I already solved it by writing my own helper function that uses the contentservice to get the propertygroups and types, and then references the types to the typeAliases of the published content propetties.

    public static List<PropertyModel> GetProperties(IPublishedContent publishedContent, params string[] propertyGroupName)
        {
            var properties = new List<PropertyModel>();
            var service = new ContentService();
            var content = service.GetById(publishedContent.Id);
            var groups = content.PropertyGroups.Where(pg => propertyGroupName.Contains(pg.Name));
    
            foreach (var group in groups)
            {
                foreach (var propertyType in group.PropertyTypes)
                {
                    var prop = publishedContent.GetProperty(propertyType.Alias);
                    if (prop != null && prop.HasValue)
                    {
                        var propertyModel = new PropertyModel(
                            propertyType.Name,
                            prop.Value.ToString()
                        );
    
                        properties.Add(propertyModel);
                    }
                }
            }
    
            return properties;
        }
    

    I wouldn't say this is slow though as my pagespeed according to Google is 81 / 100 and I have not even optimized any css/js or caching yet.

    But, to be clear on what my usecase is.

    I am working on a website for an animal shelter. They have an overview of animals where each species of animals have unique properties, but they also share certain properties.

    What I did, was make a composite document type for the shared properties, and made a specific document type for each specific species. Then I put all properties for the animals under a "Properties" tab. So now I need to read all properties for the animals on the page, as all document types use the same basic html template.

Please Sign in or register to post replies

Write your reply to:

Draft