Search code examples
c#sitecoresitecore6

Sitecore Item Bucket queries all return null


I am implementing Sitecore Item Buckets into my project for Sitecore 6.4, .NET 2.0.

I have everything installed and all appears to be working correctly. I created an item bucket and a bucketable template. The Search UI works perfectly in Content Editor queries. I can also query the index for my items in both Sitecore Index Viewer as well as the Sitecore Rocks Index utility.

However when I try to implement the UI in code, I can never get any results no matter what method I try. I keep getting null back for every type of query I try to run. I've tried BucketManager, Search extension method, and BucketQuery all to no avail.

I also have Debug logging turned on and when I search in Content Editor via the Search tab I see logging for the query, but when my code executes no log entries are generated whatsoever. Does anyone have an idea of why this may be happening. More than happy to provide more information to try and track this down.

Item root = MasterDatabase.GetItem(Constants.ARI_BUCKET_LOCATION_ID); 

var items = root.Search(out hitCount, 
text: "*", 
indexName: "itembuckets_buckets", 
location: root.ID.ToString(), 
language: "en", 
startDate: "01/01/2013", 
endDate: "12/31/2013", 
numberOfItemsToReturn: 100, 
pageNumber: 1, 
templates: tmpInventoryRate.ID.ToString()); 

var itemresults = root.Search(out hitCount, numberOfItemsToReturn: 100, language: "en"); 

var results = BucketManager.Search(root, out hitCount, templates: tmpInventoryRate.ID.ToString()); 

var textresults = BucketManager.Search(MasterDatabase.GetItem(Constants.ARI_BUCKET_LOCATION_PATH), out hitCount, text: "OEH", location: root.ID.ToString()); 

var pathresults = BucketManager.Search(MasterDatabase.GetItem(Constants.ARI_BUCKET_LOCATION_PATH), out hitCount, templates: tmpInventoryRate.ID.ToString()); 

var queryresults = new BucketQuery().WhereTemplateIs("*").Run(root, 100); 

One thing to note that I am curious about is that the above code executes in a DAL module that does not have access to Sitecore.Context but the MasterDatabase.GetItem() call does in fact retrieve the Item from the master database, but I don't know if somewhere in the Bucket API code Context is being referenced potentially?


Solution

  • OK, so after posting I followed down the path of my last comment about Context and using Reflector to dig into the Sitecore.ItemBucket.Kernel.Util.IndexSearcher and Sitecore.ItemBucket.Kernel.Managers.BucketManager classes I saw that both indeed references the Context item. Part of my problem was I started with the Item Buckets on a server side processing script that was not part of the Sitecore content tree, therefore doesn't go through the processing pipeline, so that is why I did not have Context available during execution.

    Buried in the answers here I found the way to set current context programatically from web.config <site> setting: Set Active Site as Context

    Using that I then used the following using block to set the desired site as current Context like so:

                int hitCount;
                Item root = MasterDatabase.GetItem(Constants.ARI_BUCKET_LOCATION_ID);
    
                using (new SecurityDisabler())
                {
                    Sitecore.Context.SetActiveSite("website"); //Set Current Context
                    var items = root.Search(out hitCount,
                                        text: "*",
                                        indexName: "itembuckets_buckets",
                                        location: root.ID.ToString(),
                                        language: "en",
                                        startDate: "01/01/2013",
                                        endDate: "12/31/2013",
                                        numberOfItemsToReturn: 100,
                                        pageNumber: 1,
                                        templates: "{3B0476F4-C3C4-43DD-8490-2B3FF67C368B}");
                }
    

    After making this change, I received my bucket items as expected!!

    SIDE NOTE: Also just as a note for anyone who may be going down the same path following the code examples that was unrelated to this issue but something I came across while following the code samples. Make sure you have your <site name='website' ... content='master' > setting set properly in you web.config to use the Sitecore.Context.ContentDatabase.GetItem() methods as used in the developer guide and I also saw some references in the Searcher class that referenced Context.ContentDatabase as well.

    Hope this saves someone else a little more time than it took me!