Quantcast
Viewing all articles
Browse latest Browse all 43

Sitecore Context Database is Null, What is Your Context?

I frequently receive questions about the context database in Sitecore and about what the content database is and how it relates to the context database. So here is a small breakdown of the database structure in Sitecore and how it relates to context.

Most developers are familiar with the context database that can be reached through Sitecore.Context.Database but also find themselves suprised from time to time when they find that the context database is either null or pointing to another database than expected in different scenarios. So why is it that the database is null in some cases?

Well it all has to do with site context. All requests to Sitecore are served within the context of a site and these are set up in the sites section of the Sitecore configuration. So if we take a look at the site definitions it is worth noting that they all contain different attributes that defines when the specific site should be set as the context site. This can for instance be because of a matching hostname or a matching physical file path. So for instance a request for a path beneath /sitecore/shell will set the shell website as the context site and therefore use the information specified for this site definition including information about the context database which in this case is the core database.

So if you were to create a stand alone aspx page and place it under /sitecore/admin it will run in the admin site context which actually doesn’t have a context database specified which means that if you try to reference Sitecore.Context.Database it will be null. However if you move the same page to somewhere beneath /sitecore modules/web the context database will point to web as it will now be running under the modules_website site context where the context database is set to web. In cases where the code is executed as part of a scheduled task the context database will also be null since it is not run in the context of a request and therefore also not in the context of a site.

So now that we know that the context database is only available within a request and is determined by the site context let us take a look at what the content database is.

The content database is also controlled through the site definitions and it is really only used and relevant in conjunction with the different Sitecore clients such as the content editor and the page editor sine they are also running within a site context - in particular the shell site. This as well as the modules_shell site specifies not only a context database but also a content database where the context database is pointing to the core database and the content database is pointing to the master database. This is because the Sitecore clients draws their configuration data from the core database but at the same time also needs to specify which database the content that the clients display and edit should come from. So in short, if your code is running in the context of a Sitecore client for instance as part of an event triggered in the content editor then you should reference Sitecore.Context.ContentDatabase instead of Sitecore.Context.Database.

A usefull tip is to write your code so that it will first try to use the content database and default to the context database if the content database is null.

Database database = Sitecore.Context.ContentDatabase ?? Sitecore.Context.Database;

Hoepfully this will shed some light on the relation between the context and the content database and help illiminate som confusion.


Viewing all articles
Browse latest Browse all 43

Trending Articles