2

When I call Sitecore.Context.Database inside of an IComputedIndexField, it returns Core. I can't use this to call the GetItem() function.

Thus, I wrote the following as a workaround.

var indexableItem = indexable as SitecoreIndexableItem;
if (indexableItem == null)
                {
                    Sitecore.ContentSearch.Diagnostics.CrawlingLog.Log.Warn(this + " : unsupported IIndexable type : " + indexable.GetType());
                    return false;
                }
var category = indexableItem.Item.Database.GetItem(new ID("MY ID"));

This works perfectly fine, but is it the correct way to call the Database object?

3
  • 1
    I can confirm this is how I get items other than the current Indexable.
    – vandsh
    Nov 6, 2017 at 20:55
  • 1
    I use "indexableItem.Item.Database" too. Nov 6, 2017 at 21:09
  • 1
    looks good to me Nov 6, 2017 at 21:57

1 Answer 1

2

The code you posted is the right way to get the database based on the database of the item being indexed, I think you should get null for Sitecore.Context.Database because the code is NOT running within any site context, Check Sitecore.Context.Site to verify (Thats what i see in Sitecore 8.2u5), Maybe in older version, it was running with the context of "shell" site, which uses the core database

<site name="shell" database="core"  virtualFolder="/sitecore/shell" physicalFolder="/sitecore/shell" rootPath="/sitecore/content" startItem="/home" language="en" ..... />
1
  • Great point. You were right, under the shell site, my database is set as core. Now, I understand why the Sitecore.Context.Database is set to core. Nov 7, 2017 at 14:08

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.