Copied to clipboard

Flag this post as spam?

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


  • Craig O'Mahony 364 posts 918 karma points
    Nov 06, 2014 @ 09:42
    Craig O'Mahony
    0

    Loop through children using the ContentService

    Hi folks.

    Could someone tell me if it's possible to loop through all of the children of a node using the ContentService API please?

    For instance what I'm trying to do is loop through every child of a parent node (id 1055) and check it's UpdateDate property.

    Using Nodes I can loop through the node.Children collection but I can't seem to find one for the ContentServive.

    This is what I have to get the Parent Node.

            var contentService = new ContentService();
            var content = contentService.GetById(1055);
            int myDate = Int32.Parse(content.UpdateDate.DayOfYear.ToString());

    But having got this I'd like to loop through the children. 

    Could anybody help please?

    thanks,

    Craig

  • jivan thapa 194 posts 681 karma points
    Nov 06, 2014 @ 10:50
    jivan thapa
    0

    What's about this one.

     var contentService = ApplicationContext.Current.Services.ContentService;
        foreach (var childContent in contentService.GetById(1055).Children())
        {
    
        }
    
  • Craig O'Mahony 364 posts 918 karma points
    Nov 06, 2014 @ 11:14
    Craig O'Mahony
    0

    Hi Jivan,

    Thanks but the Umbraco.Core.Models.IContent doesn't contain a definition for 'Children'

    Craig

  • Craig O'Mahony 364 posts 918 karma points
    Nov 06, 2014 @ 11:27
    Craig O'Mahony
    100

    Sussed it.

    var contentService = new ContentService();

            string myString = "";

            var cPages = contentService.GetChildren(1055);

            foreach (var item in cPages)

            {

                //Do stuff in here

            }

    All good!

  • jivan thapa 194 posts 681 karma points
    Nov 06, 2014 @ 14:26
    jivan thapa
    0

    Anyway, You found the ans.

    There is a def. for "Children", you may missing some stuff. You can compare with this one

    enter image description here

Please Sign in or register to post replies

Write your reply to:

Draft