8

I have following structure in sitecore

> Country 
    > India
    > Srilanka 
    > Bangladesh

The following query returns all the child items but in alphabetical order

 string parentItemPath = "fast:/"+ SitecoreHelper.GetItemUnversioned(Model.Item.ID.ToString()).Paths.Path;
 Item[] allItems = SitecoreHelper.GetVersionedItemsByQuery(parentItemPath + "/*" );

But I need the child items in the order defined in Sitecore.
i.e, Same as like Content Tree Order.Is there any way to achieve this.

UPDATE: I have found one solution, not sure whether it is efficient or not Provide values for sort field in each Item, like 200 to India, 201 to Srilanka and so on, and view the same in View. They are listed as same number.

3

2 Answers 2

9

Fast query runs on the database directly. In most of the scenarios it's not an ideal choice.

If you want to get children in the same order as they are defined in Sitecore, use Sitecore API.

Model.Item.Children
Model.Item.GetChildren()

Item.GetChildren() method can take ChildListOptions as the parameter. Here are available flags:

ChildListOptions.None
ChildListOptions.IgnoreSecurity
ChildListOptions.AllowReuse
ChildListOptions.SkipSorting
4
  • 13
    Don't. Use. Fast:/. Query. For. Anything. Ever.
    – Mark Cassidy
    Commented May 18, 2018 at 9:42
  • Thanks for the quick update.I will check and update the same
    – rams
    Commented May 18, 2018 at 9:45
  • @MarkCassidy Is there any specific reason you said don't use fast query even we know fast query is faster than sitecore query
    – Sukhjeevan
    Commented Jun 15, 2018 at 13:05
  • 1
    It is not. Absolutely not. The only case where fast query is faster, is on your local 1-man development machine. It does not scale, it KILLS your SQL server and it should be avoided at all times.
    – Mark Cassidy
    Commented Jun 15, 2018 at 13:12
7

Welcome to Sitecore.

If you want to have the first level children of an item use:

selectedItem.GetChildren()

There are many ways to implement this so below is just one example:

var countryItem = Database.GetDatabase("master").GetItem(countryItemID);
if (countryItem != null)
    var allFirstLevelChildren = countryItem.GetChildren();

This should return all items as they are on Sitecore Content Tree.

If you change your mind and want to sort this list or manipulate it then you can use LINQ for example:

var items = countryItem.GetChildren().Where(x => x.TemplateID == "{CountryTemplateId}");

or

var items = countryItem.GetChildren().OrderBy(x => x.Name);

Now, if you want to have ALL levels children of an item, again there are different ways to implement this but particularly I like to use the one below:

Database.GetDatabase("master").SelectItems(countryItem.Paths.Path + "//*").ToList();

Another very common function to get all levels of children is:

countryItem.Axes.GetDescendants();

I hope that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.