I have an item bucket, and what is the best way to get all their children programatically?
Thank you
The best way would be to leverage the Sitecore.ContentSearch
APIs to search for all items
public List<T> GetAllItemsFromBucket(string bucketPath)
{
var index = ContentSearchManager.GetIndex("{index name}");
using (var context = index.CreateSearchContext())
{
var results = context.GetQueryable<T>().Where(x => x.Path.StartsWith(bucketPath)).ToList();
foreach (var result in results) _service.Map(result);
return results;
}
}
Depending on where you are using this logic you can either setup a custom index or leverage one of the built in sitecore indexes (sitecore_master_index
or sitecore_web_index
)
For more information on how to setup and use the Sitecore.ContentSearch API, you can read more at the Sitecore Documentation Search section
To add to Patricks answer, you can also instantiate a search context using the root bucket item:
given Item bucketItem
IIndexable index = new SitecoreIndexableItem(bucketItem);
using (var context = ContentSearchManager.GetIndex(index).CreateSearchContext())
{
...
}
I'm adding this as an answer as it wouldn't fit as a single comment.
One thing that it's important to take into consideration is that when you use buckets the parent-child relationship between the items gets removed, and instead the items are organized in a folder structure according to the date and time they were created. Therefore, the item buckets feature is primarily designed for storing content items that do not need to be stored in a hierarchy. You can read more about buckets here.
So to the questions's original comment on getting the children what you are really getting is all the buckets for that item.
Now how do you get them? Just make a Content search call and I think Patrick's answer pretty much covers how you do that.