-2

For a project I am working on, I am attempting to figure out how to loop through a bucketed folder programmatically using C#. In viewing the bucketed folder structure, the bucket folder nests the items quite deep

ex.

2018\
     04\
        09\
           32\
              16\
                 Item A
                 Item B
                 Item C

and I'm familar with how to loop through something one level deep in c#, but not something that is 5 or 6 levels deep.

@foreach (var child in Model.Children)
{
    var mediaItem = (MediaItem)child;
    ....
 }

This style of looping is fine for a parent folder\child relationship, but doesn't drill down multiple levels to get to the items at the bottom.

I'm pretty new to c#, so I could really use a point in the right direction on how to drill down to something that is multi-levels deep and then loop through them.

thanks!

  • 1
    You should add a little bit of context. The main purpose of Sitecore Buckets is, to not actually loop through all of the content of them; but rather use ContentSearch technology to find the items you need. I'm sure someone can and will show you a recursive loop, but you might be heading down the entirely wrong path here. – Mark Cassidy Apr 9 '18 at 15:21
  • Thanks Mark, I will attempt to do that. The project requires me to retrieve the items programatically, and not through a search style function in the editor. – Nick Apr 9 '18 at 15:24
  • What is your actual requirement? – Gaurav Agarwal Apr 9 '18 at 15:24
  • The whole idea with buckets is to allow storing of millions of items... Mark is correct, you should be using the Content Search API to retrieve the items from code, e.g. sitecore.stackexchange.com/q/6057/135 – jammykam Apr 9 '18 at 15:41
  • Your question is a clear duplicate. However - All you really need is .GetItem for the GUID that your config file states. No looping required. – Mark Cassidy Apr 9 '18 at 15:45
0

You can use code like this

if (item.TemplateID.Guid ==YourItemTemplateId
                && item.Parent != null)
            {
                var bucketroot = item.Parent;
                while (bucketroot.TemplateID == ID.Parse(Constants.Templates.BucketTemplateId))
                {
                    bucketroot = bucketroot.Parent;
                }
}

It is based on your requirement what you want.

| improve this answer | |
  • That looks like it just might be the thing Gaurav.. so would that drill down to the bottom of parent folder and return a child list of items? :) – Nick Apr 9 '18 at 15:30
  • The requirement is that I need to be able to retrieve items from sitecore's content tree via Guid's that are stored in a config file. So for example, the bucket folder has a guid id, which would be stored in a config file, then the c# code would grab that guid, drill down through the bucket folders to find the items, and then access the items in the folder by their guid's. – Nick Apr 9 '18 at 15:34
  • 1
    You can grab any item directly via that GUID however. And it will be like a gazillion times more efficient. – Mark Cassidy Apr 9 '18 at 15:36
  • 1
    That said; @Guarav, that code is like... way out there. Nowhere near something that will function. It loops UP the recursion, for starters. – Mark Cassidy Apr 9 '18 at 15:41
  • Please use the Content Search API, buckets were designed to use that: sitecore.stackexchange.com/q/6057/135 – jammykam Apr 9 '18 at 15:42

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