Copied to clipboard

Flag this post as spam?

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


  • Martin Rud 261 posts 1002 karma points c-trib
    Dec 19, 2020 @ 15:04
    Martin Rud
    0

    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

    • property name
    • datatype
    • value

    for each property?

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Dec 20, 2020 @ 13:14
    Huw Reddick
    0

    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() +"\"}");
                }
            }
    
        }
    
    }
    
  • Malthe Petersen 68 posts 383 karma points c-trib
    Dec 20, 2020 @ 14:08
    Malthe Petersen
    101

    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

  • Martin Rud 261 posts 1002 karma points c-trib
    Dec 20, 2020 @ 17:18
    Martin Rud
    0

    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. :)

Please Sign in or register to post replies

Write your reply to:

Draft