While working with Sitecore, a question struck my mind. It was about the two methods of sitecore to fetch its sub-items or children.
The question was what is the difference between the two methods and why do we have these two methods separately :(.
I was sure of one point, that if these two methods are returning the same thing, there has to be a pretty valid reason as to why are they separately designed.
So I started googling and the findings are as under:
- GetChildren() :
1. This method can be accessed as follows: itemObject.GetChildren();
2. Returns all the direct children of an item –i.e. All the items present at level 1 under the item.
3. If you need all the children, you need to recursively call GetChildren() method, for all the items under a specific item. — This surely gonna consume more time!
(Also, the same result as GetChildren can be found using some XPath query too, but not here… As this is simply differentiating between GetChildren and GetDescendants.)
1. This method can be accessed as follows: itemObject.Axes.GetDescendants();
2. Returns all the items under an item.
3. If you want all the children, just one go.. Call GetDescendants() method and you have them all…! No recursive calls! 🙂 — So that’s where this method seems good! 😉
So ready to test it? 🙂
To do so, I created an ASPX page in Sitecore Tools!
The source code of the page will be available soon.
Just to make it clear, I am first taking the count for GetChildren (9) and then the count for GetDescendants (76) for the same item.
Next, is the names of list of items in GetChildren and GetDescendants.
Hope this helps!
Happy Sitecoring! 🙂