Copied to clipboard

Flag this post as spam?

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


  • Biagio Paruolo 1594 posts 1825 karma points c-trib
    Oct 29, 2023 @ 09:14
    Biagio Paruolo
    0

    How to get all proprieties children of a tab into a document type?

    How to get all proprieties children of a tab into a document type? That is: I've a document type and I wish to read al fields and proprieties of a "tab". I this manner I don't have manually to write each alias in the code. Thanks. bp

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Oct 29, 2023 @ 19:54
    Marc Goodson
    0

    Hi Biagio

    Just to clarify, are you talking on the front end in a template? or in the backoffice via events / angularJS?

    And if on the front end, are you using Modelsbuilder, eg Model.PropertyName or Model.Value("propertyNameAlias") to write out properties?

    Essentially on the front end of the site it is trying to be as performant as possible, so the information you get by default about the Document Type behind a page doesn't care too much about how those properties are organised in the backoffice, or which tab the are on.

    If you using Model.Value, then you also have Model.Properties which is an IEnumerable of all the properties on the Model of IPublishedProperty type...

    ... so you could loop through those and read the alias, but you wouldn't know which tab it was on...

    foreach (var property in Model.Properties) {
    <li>@property.Alias | @property.GetValue()
    }
    

    I've seen naming conventions of Property Aliases in the past, so they all start with the same prefix, so they can be written out in groups in this way...

    so if your tab were 'Meta Data', you wanted 5 properties to be written out as bullet points, you could name each of those property alias as beginning with 'meta', and use the foreach loop above like this

    foreach (var property in Model.Properties.Where(f=>f.Alias.StartsWith("meta")) {
    <li>@property.Alias | @property.GetValue()
    }
    

    If you were using Modelsbuilder, and if all the properties on your tab were added via a Composition then you would have an Interface you could cast your Model to, which would only contain those properties.

    var metaDataModel = Model as IMetaDataComposition
    

    All that said, technically what you ask, isn't impossible though... there is a way but it generally isn't considered good practice, (or at least not without caching, as it would hit the database direct rather than use the published cache...

    ... but academically.... you could get the current page's ContentType Id from the Model, eg

    var contentTypeId = Model.ContentType.Id;
    

    Then you could inject and use the ContentTypeService to get an IContentType representing the Document Type of the current page's Model eg

    var contentType = _contentTypeService.Get(contentTypeId);
    

    and then this IContentType has a property called 'PropertyGroups' which is a collection of the 'Tabs' and so you could get the tab of the properties you want to render eg

    var myTab = contentType.PropertyGroups.FirstOrDefault(f => f.Alias == "myTabAlias");
    

    and then the Tab has a list of it's defined PropertyTypes so you could loop through this eg

        foreach (var tabProperty in myTab.PropertyTypes)
        {
            @tabProperty.Alias - @Model.Value(tabProperty.Alias)
        }
    

    or at least that would be the gist!

    regards

    Marc

Please Sign in or register to post replies

Write your reply to:

Draft