Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
Hi,
In a template I want to loop through all nodes of a node/doc type (in order to output that node as json).
How do I loop properties and get
for each property?
Don't know if this will help, but may give you an idea.
I wanted to export all the meta descriptions from all the pages in my site, so this is basically what I did
public ActionResult ExportMetaData() { int count = 0; List<string> pages = new List<string>(); IPublishedContent homePage = Umbraco.Content(Guid.Parse("a53c075a-61a2-456e-a73d-e65e52efea92")); CountNodes(homePage.Children,ref pages, ref count); JsonResult ret = Json("{\"Synchronised\":[" + string.Join(",", pages.ToArray()) + "]}"); return ret; } private void CountNodes(IEnumerable<IPublishedContent> children, ref List<string> pages, ref int count) { count += children.Count(); foreach (IPublishedContent item in children) { var test = (PublishedContentModel)item; if(item.Children != null) { if (test.GetProperty("metaName") != null) { pages.Add("{\"Id\":\"" + test.Id + "\",\"Name\":\"" + test.Name + "\",\"metaName\":\"" + test.GetProperty("metaName").GetValue().ToString() + "\",\"Description\":\"" + test.GetProperty("metaDescription").GetValue().ToString() +"\"}"); } CountNodes(item.Children, ref pages, ref count); }else{ if (test.GetProperty("metaName") != null) { pages.Add("{\"Id\":\"" + test.Id + "\",\"Name\":\"" + test.Name + "\",\"metaName\":\"" + test.GetProperty("metaName").GetValue().ToString() + "\",\"Description\":\"" + test.GetProperty("metaDescription").GetValue().ToString() +"\"}"); } } } }
Hi Martin.
If you wish to loop through all properties defined by the document type, including compositions, you can simply:
var content = UmbracoContext.Content.GetById(1234); var properties = content.Properties;
That will give you all properties. A property is of type IPublishedProperty, check out the interface for more info on what it contains: https://github.com/umbraco/Umbraco-CMS/blob/v8/contrib/src/Umbraco.Core/Models/PublishedContent/IPublishedProperty.cs
A good tip for the future: get comfortable in Umbracos source code at least the IPublished* part, that will help you a lot in the future and save you a lot of time, when you need to do something that is a little more advanced. :-)
Hope my answer can help you,
Regards
Thank you, both Huw and Malthe,
I found out (with help from the Facebook-group) that the answer was .Properties as you suggest, Malthe:
foreach (var property in Model.Properties) { // Model.Value(property.Alias) }
(my task was making a template to use as altTemplate for every page in order do output json with the data of this page)
I will look more into IPublished and like in the docs also. :)
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.
Continue discussion
How to loop all properties of a document?
Hi,
In a template I want to loop through all nodes of a node/doc type (in order to output that node as json).
How do I loop properties and get
for each property?
Don't know if this will help, but may give you an idea.
I wanted to export all the meta descriptions from all the pages in my site, so this is basically what I did
Hi Martin.
If you wish to loop through all properties defined by the document type, including compositions, you can simply:
That will give you all properties. A property is of type IPublishedProperty, check out the interface for more info on what it contains: https://github.com/umbraco/Umbraco-CMS/blob/v8/contrib/src/Umbraco.Core/Models/PublishedContent/IPublishedProperty.cs
A good tip for the future: get comfortable in Umbracos source code at least the IPublished* part, that will help you a lot in the future and save you a lot of time, when you need to do something that is a little more advanced. :-)
Hope my answer can help you,
Regards
Thank you, both Huw and Malthe,
I found out (with help from the Facebook-group) that the answer was .Properties as you suggest, Malthe:
(my task was making a template to use as altTemplate for every page in order do output json with the data of this page)
I will look more into IPublished and like in the docs also. :)
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.