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.
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
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
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?
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...
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
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.
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("-----");
}
}
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:
How to get the field name and not the alias? Thanks.
This the element type:
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
regards
Marc
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?
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
The alias of the element like “codicePiano” ( see the image above ).
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
fingers crossed!
regareds
Marc
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.
This works, but I don't like very much...
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"
I found another better solution:
Hi Biagio
Yes, the ContentTypeService has an overload for the key too... glad you got there!
regards
Marc
is working on a reply...