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
    Nov 02, 2023 @ 17:18
    Biagio Paruolo
    0

    Block and BlockListItem: Help..

    Hi,

    I create a document type with a Block List.

    I'm able to write a block into a blocklist and save it following your guide ( https://docs.umbraco.com/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/block-editor/block-list-editor ).

    After I wish to get the list and items for writing a report.

    I create this code:

       var pu = _publishedContentQuery.Content(5686);
     IEnumerable<BlockListItem> lista = new List<BlockListItem>();
     lista = (IEnumerable<BlockListItem>)pu.GetProperty("righePreventivo").GetValue();
     foreach (var item in lista.Select(x => x.Content))
     {
         foreach (var item2 in item.Properties)
         {
             Debug.WriteLine("-----");
             Debug.WriteLine(item2.Alias);
             Debug.WriteLine(item2.GetValue(item2.Alias));
         }
     }
    

    How to get the field name and not the alias? Thanks.

    This the element type: enter image description here

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Nov 03, 2023 @ 13:15
    Marc Goodson
    0

    Hi Biagio

    When you are querying the published cache of Umbraco, it's optimised to contain the most efficient amount of information for building a website.

    Consequently, when you are looping through the 'Properties' of an Element Type, like in your example, you will find you are being given reference to IPublishedProperty objects

    https://github.com/umbraco/Umbraco-CMS/blob/e17fa957d6c6a0ee38e5c07bcb43c26b1fe4cae2/src/Umbraco.Core/Models/PublishedContent/IPublishedElement.cs#L35

    And as you have found, these objects do not include the Name as presented in the backoffice:

    https://github.com/umbraco/Umbraco-CMS/blob/e17fa957d6c6a0ee38e5c07bcb43c26b1fe4cae2/src/Umbraco.Core/Models/PublishedContent/IPublishedProperty.cs#L6

    Because normally you wouldn't need to render that name anywhere in your site...

    ... But there is a way to get the full information of an Element Type including the name and that is to use the ContentTypeService - but be aware - these services hit the database and so if used on the front end, need to be cached, otherwise will be super slow!

    Anyway, if you inject the ContentTypeService into your code you'd be able to write something like

    IContentType contentTypeInfo = contentTypeService.Get(item.Alias);
    foreach (var propertyType in contentTypeInfo.PropertyTypes) {
         Debug.WriteLine("-----");
         Debug.WriteLine(propertyType.Name);
         Debug.WriteLine(item.GetValue(propertyType.Alias)));
    }
    

    regards

    Marc

  • Biagio Paruolo 1594 posts 1825 karma points c-trib
    Nov 03, 2023 @ 15:17
    Biagio Paruolo
    0

    Thanks to reply me. The performance is not a problem. I’ve a lots of fields and I’ve to create a pdf report. Then I don’t want to write in the code the field names. I made a simple test. I pass the field alias ( the field of the elemet type of the block item ) into the content type service but it return a null value. Wht?

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Nov 03, 2023 @ 15:46
    Marc Goodson
    0

    Hi Biagio

    When you say 'field alias' do you mean an alias of a property or the alias of the Element Type...

    The alias of the Element Type should bring something back!

    But if not then I think there is an overload of contentTypeService Get that works with the Guid of the Element Type (if you look for the guid in the backoffice, you could try hardcoding that in a test and confirm it's bringing back the ContentType ok and you can loop through the PropertyTypes successfully...

    regards

    Marc

  • Biagio Paruolo 1594 posts 1825 karma points c-trib
    Nov 03, 2023 @ 16:15
    Biagio Paruolo
    0

    The alias of the element like “codicePiano” ( see the image above ).

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Nov 03, 2023 @ 16:38
    Marc Goodson
    0

    Hi Biagio

    In the image above 'codicePiano' appears to be a text field...

    The ContentTypeService works with Element Types and Document Types

    so you'll need the alias for 'Preventivo Row Item' (it's not in the screenshot, but should be on the right hand side of the screen) possibly it is just 'preventivoRowItem'

    Then you will be able to do a foreach over it's PropertyTypes

    IContentType preventivoRowItem = contentTypeService.Get("preventivoRowItem");
    foreach (var propertyType in preventivoRowItem.PropertyTypes) {
         Debug.WriteLine("-----");
         Debug.WriteLine(propertyType.Name);
         Debug.WriteLine(item.GetValue(propertyType.Alias)));
    }
    

    fingers crossed!

    regareds

    Marc

  • Biagio Paruolo 1594 posts 1825 karma points c-trib
    Nov 03, 2023 @ 18:31
    Biagio Paruolo
    0

    I attach the document type images because there is something that don't work. I pass the alias of the BlockList item that is "righePreventivo" and I've the info, but the value is the json list.

    enter image description here enter image description here enter image description here

  • Biagio Paruolo 1594 posts 1825 karma points c-trib
    Nov 03, 2023 @ 20:17
    Biagio Paruolo
    0

    This works, but I don't like very much...

    var pu = _publishedContentQuery.Content(5686);
            IEnumerable<BlockListItem> lista = new List<BlockListItem>();
            lista = (IEnumerable<BlockListItem>)pu.GetProperty("righePreventivo").GetValue();
            var listaaa = lista.Select(x => x.Content);
            foreach (var item in listaaa)
            {
                IContentType contentTypeInfo = _contentTypeService.Get(item.ContentType.Alias);
                foreach (var item2 in item.Properties)
                {
                    var valore = contentTypeInfo.PropertyTypes.Select(x => x.Alias == item2.Alias).FirstOrDefault();
                    var nome = "";
                    foreach (var propertyType in contentTypeInfo.PropertyTypes)
                    {
                        if (propertyType.Alias == item2.Alias)
                        {
                            nome = propertyType.Name;
                            break;
                        }
                    }
                    Debug.WriteLine(nome);
                    Debug.WriteLine(item.Value(item2.Alias));
                    Debug.WriteLine("-----");
                }
            }
    
  • Biagio Paruolo 1594 posts 1825 karma points c-trib
    Nov 03, 2023 @ 21:08
    Biagio Paruolo
    0

    Is't not possible to use contentUdi or contentTypeKey to query the type or data of BlockList?

    {"layout":{"Umbraco.BlockList":[{"contentUdi":"umb://element/94548b86e9f945baaeb1bf93b166473f"}]},"contentData":[{"contentTypeKey":"483395c5-44c0-4893-bac0-e69c3cbf9428","udi":"umb://element/94548b86e9f945baaeb1bf93b166473f"

  • Biagio Paruolo 1594 posts 1825 karma points c-trib
    Nov 03, 2023 @ 21:54
    Biagio Paruolo
    0

    I found another better solution:

    if (p.PropertyType.PropertyEditorAlias == "Umbraco.BlockList")
                            {
                                IEnumerable<BlockListItem> lista = new List<BlockListItem>();
                                List<TableRowType> elenco = new List<TableRowType>();
    
                                BlockValue blockValue =  JsonConvert.DeserializeObject<BlockValue>(_content.GetValue(p.Alias).ToString());
    
                                if (blockValue is null) continue;
    
                                foreach (var item in blockValue.ContentData)
                                {   var contentType = _contentTypeService.Get(item.ContentTypeKey);
    
                                    foreach (var itemProperty in item.RawPropertyValues)
                                    {
                                        IContentType contentTypeInfo = _contentTypeService.Get(contentType.Alias);
    
                                        var nome = "";
                                        foreach (var propertyTypeLocal in contentTypeInfo.PropertyTypes)
                                        {
                                            if (propertyTypeLocal.Alias == itemProperty.Key)
                                            {
                                                nome = propertyTypeLocal.Name;
                                                break;
                                            }
    
    
                                        }
    
                                        Debug.WriteLine(nome);
                                        Debug.WriteLine(itemProperty.Value);
                                        Debug.WriteLine("-----");
    
                                        TableRowType tableRowTypeBlock = new TableRowType();
                                        tableRowTypeBlock.tipoRiga = "rigablock";
                                        tableRowTypeBlock.titolo = nome;
                                        tableRowTypeBlock.tipoContainerDataType = p.PropertyType.PropertyEditorAlias;
                                        tableRowTypeBlock.titolocella = nome;
                                        if (itemProperty.Value is string)
                                            tableRowTypeBlock.valorecella = itemProperty.Value.ToString();
    
                                        elenco.Add(tableRowTypeBlock);
                                    }     
                                }
       }
    
  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Nov 04, 2023 @ 16:50
    Marc Goodson
    0

    Hi Biagio

    Yes, the ContentTypeService has an overload for the key too... glad you got there!

    regards

    Marc

Please Sign in or register to post replies

Write your reply to:

Draft