How to get the name of a property via IPublishedProperty ?
When looping over a node Properties property (a collection of IPublishedProperty object), I was hoping I could get the property name along with its value. But to my surprise, IPublishedProperty does not offer this data. Only the property alias and its value.
How do I get then the node's properties names ?
I need to return a response with a key/value list of the node's properties, by name and value.
I am already using the umbraco helper, more precisely, its TypedContent method, by which I get one IPublishedContent object. I then loop over the Properties of the latter, which means I loop over IPublishedProperty elements.
So, my problem is: how to access the property's name? IPublishedProperty does not expose it!
Here's my code. For the time being, I am using PropertyTypeAlias, but I need the property name, instead.
List<dynamic> nodeProperties = new List<dynamic>();
IPublishedContent content = Umbraco.TypedContent(umbracoNodeId);
if (content != null)
{
foreach (IPublishedProperty property in content.Properties)
{
nodeProperties.Add(new { key = property.PropertyTypeAlias, value = property.Value });
}
}
var docType = DocumentType.GetByAlias(umbNode.NodeTypeAlias);
foreach (var propertyType in docType.PropertyTypes)
{
var propName = propertyType.Name;
var propValue = umbNode.GetProperty(propertyType.Alias).Value;
}
DocumentType type is obselete. I am using instead Services.ContentTypeService.GetContentType, which returns IContentType, which contains a collection of PropertyType objects at "PropertyTypes".
Another thing: if anyone else ends up here and, like me, is interested in a given document type properties only - and not the inheriting ones - then IContentType.PropertyTypes will give you just that.
How to get the name of a property via IPublishedProperty ?
When looping over a node Properties property (a collection of IPublishedProperty object), I was hoping I could get the property name along with its value. But to my surprise, IPublishedProperty does not offer this data. Only the property alias and its value.
How do I get then the node's properties names ? I need to return a response with a key/value list of the node's properties, by name and value.
Thanks in advance.
Anyone?
Hi Avraham,
have you look Umbraco Helper , kindly check below link
https://our.umbraco.org/documentation/Reference/Querying/UmbracoHelper/
i am not sure but it will help to get node and its property by name.
hope this will help you.
Regards,
Mehul Gajjar.
I am already using the umbraco helper, more precisely, its TypedContent method, by which I get one IPublishedContent object. I then loop over the Properties of the latter, which means I loop over IPublishedProperty elements. So, my problem is: how to access the property's name? IPublishedProperty does not expose it!
Here's my code. For the time being, I am using PropertyTypeAlias, but I need the property name, instead.
Hi Avraham,
kindly check the below
Hope this will help you.
Regards,
Mehul Gajjar.
Nice! Thanks Mehul! This is what I needed :-)
Just a minor improvement to your answer:
DocumentType type is obselete. I am using instead Services.ContentTypeService.GetContentType, which returns IContentType, which contains a collection of PropertyType objects at "PropertyTypes".
Another thing: if anyone else ends up here and, like me, is interested in a given document type properties only - and not the inheriting ones - then IContentType.PropertyTypes will give you just that.
Hi,
Glad to hear that you got what you want.
Cheers,
Mehul Gajjar.
is working on a reply...