Count Child Items in Sitecore Content Tree Without Scripts


Today, I am going to share a simple yet highly useful trick to count the number of child items in the Sitecore Content Tree without writing any scripts.

This approach was originally shared by Gabe Streza in his post, Quick Tip: Count Child Items of Sitecore Tree using Google Chrome Dev Console, where he demonstrates how to use the browser console for a quick item count. Special thanks to him for this handy trick that makes quick item counting much easier using the browser console.

When working with Sitecore, especially on large-scale implementations, there are many situations where you need to quickly count how many child items exist under a specific node. This can be helpful for content audits, migration planning, or even validating content structure during development.

The Problem

In most of the cases, developers approach this by writing Sitecore queries, using Sitecore PowerShell scripts, or implementing custom logic in C# or other languages.

While these methods are powerful but they are not always ideal when you just need a quick count.

Quick Solution Using Browser Console


Instead of writing scripts, you can use a simple JavaScript command directly in your browser.

Steps to Follow

Step 1: Open Sitecore Content Editor.
Step 2: Navigate to the item whose children you want to count.
Step 3: Expand that item in the Content Tree.
Step 4: Open browser developer tools (Press F12).
Step 5: Go to the Console tab.
Step 6: Run the following code to get the count of immediate child items.

document.getElementsByClassName('scContentTreeNodeActive')[0].nextSibling.childNodes.length

Explanation

This code works by selecting the currently active node in the Sitecore Content Tree, navigating to its child container, and counting the number of immediate child nodes. It instantly returns the count of immediate child items without requiring any backend changes.

Note: This counts only immediate children (non-recursive) and does not apply any filtering, such as template-based conditions.

Important Points to Remember

1. This counts only direct (immediate) child items.
2. Make sure the node is expanded, otherwise, the count may be incorrect.
3. This method depends on the Sitecore UI, so behavior may vary slightly across different versions.

When to Use this Approach

This approach is useful when you need:
1. Quick validation during development.
2. Instant item count without writing scripts.
3. Fast content count checks.

When Not to Use this Approach

Avoid this method if:
1. You need recursive (deep) item counts.
2. You require automation or reporting.
3. You want a reusable solution.
4. You need to filter items (e.g., by TemplateID or TemplateName).

Note: In such cases, using Sitecore PowerShell or Sitecore APIs is a better approach.

Counting child items in the Sitecore Content Tree doesn’t always require complex code or scripts. With this simple browser console trick, you can get the job done in seconds and improve your productivity while working with content structures.

References

Quick Tip: Count Child Items of Sitecore Tree using Google Chrome Dev Console - https://www.sitecoregabe.com/2017/11/quick-tip-count-children-in-part-of.html
Chirag Goel

I am a developer, likes to work on different future technologies.

Post a Comment (0)
Previous Post Next Post