Differences between GetChildren() and GetDescendants() Methods

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:

Theoretical  Explanation:

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

  • 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? 🙂

Practical Verification:

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! 🙂

Advertisements

2 thoughts on “Differences between GetChildren() and GetDescendants() Methods

  1. when you call getchildren or getdecendants and when it gets the item, does it get the entire item and all the data or does it get just an ID and lazy loads the properties when accessed? we have a large site where we are trying to build the navigation and wondering if using these functions is the best way to access the data or should be using something else. in a navigation all you need is the menu name and url of the item not the whole set of data that is in each item.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s