Recently I was working on a bit of code that would get an item from the Sitecore database by calling the Sitecore.Data.Database.GetItem(...)
method using Sitecore 8.0.
On the day I wrote the code, everything was working as intended - job done, I thought. Little did I know that the next day would give me more trouble then I asked for, since the call to the Sitecore.Data.Database.GetItem(...)
method all the sudden stopped returning my items, which lead me down a path of analyzing and debugging what was going on.
In this blog post I'll explain why this happened, how I was able to solve it, and what other things you might look out for if this happens to you.
What is causing this odd behaviour?
When I did not get any items in return from the Sitecore database, my initial thought was that someone might have deleted the item I was looking for. However, when I looked in the content tree I could see that the item was still there, so something else was going on.
While scouring the different Sitecore channels for valuable information on how to solve the issue, one of the things that came up was the fact that the data cache might be corrupted. In fact, other users have experienced data cache clearing issues causing Sitecore to believe an item doesn't exist, because it cleared the data but then didn't reload it into the cache again.
If you want to know more about how the different caching layers works in Sitecore, I would recommend that you check out Yogesh Patel's excellent blog post on how Sitecore caching work.
So how do we fix this? It happens to turn out that Sitecore has a lot of different admin pages, and one of these allows you to do a full cache refresh (using /sitecore/admin/cache.aspx
), including database prefetch, data cache, item cache, HTML cache, etc. In my case, clearing the cache immediately resolved the issue, and I was now able to retrieve the items I needed from the Sitecore database, using the very same code that failed prior to the cache clearing.
But my cache is fine, what else can I do?
If you are experiencing the same issue as me, and clearing the cache does not help you, there are other things you can try out in order to solve the problem:
-
Check that your item still exists: This one goes without saying, but if you work on a shared database with your fellow developers, chances are that someone might delete your item by accident. So before you start going down the same path as I did, do remember to verify that the item is still resting safe and sound in the content tree.
-
Check whether you are switching the context database: As a sneak peak to one of my upcoming blog posts, I will say that you should always be a bit careful when accessing the current context database, since it may not always be the database you think it is. Depending on the situation, the current context database may sometimes be the Master database, other times it may be the Core database. This means that if the data is in Master and/or Web database, you want to make sure that you are not switching over to a different context database, like Core, since you won't be able to find your item over there.
-
Check whether you are switching the language: Once you have confirmed that your item is there and you are looking in the right context database, but nothing is coming back, chances are that there is no version of the item in the language you are requesting it from. You should verify whether there is any code switching the context language programmatically, and if so you need to make sure that there is a version of the item in that specific language - if not, then you wont get any results back.
If you have other tricks on how you can get your items returned from Sitecore, or perhaps some information on why this issue happens in the first place, please drop me a note in the comment section down below.