Accessing Items from Sitecore Database in different scenarios

Heythere! Welcome to "Sitecore Series"

Post is all about sharing the details about "Accessing items from Databases and about Sitecore.Context.Database in different scenarios".

Sitecore.Context.Database.GetItem() returning null even the items exists in the Database? Want to know more details why this is not returning any results or null? You are on right place, check below for details.


Let's dig into different scenarios and ways to access database to understand more about this:

Different ways to access Databases:


1) Sitecore.Data.Database database = Sitecore.Context.Database;
2) Sitecore.Data.Database database = Sitecore.Configuration.Factory.GetDatabase("master");
3) Sitecore.Data.Database database = Sitecore.Data.Database.GetDatabase("master");

Last 2 approaches works as expected, but the issue here is we have hardcoded the database values.

In the 2&3 approaches, if the piece of code is executed from CD site it will throw an error as in most of the cases Master DB is not accessible in CD site as master db is disabled in CD app settings.


Let's understand this with an example:

A helper class contains below piece of code to return an item from db.


Sitecore.Data.Database database = Sitecore.Configuration.Factory.GetDatabase("master");
database.GetItem("item path");

Scenario 1: This helper is called in a custom command class which is added in sitecore toolbar to fetch item from db

Scenario 2: Same helper is called in a component to display some details in site.

Now, the above piece of code works as expected for scenario1 but not for scenario 2. In case of Scenario 2, master db is not accessible .

To avoid this, we can use "Sitecore.Context.Database". 


About Sitecore.Context.Database:

Scenario 1: In CD Site(Live Site)

Sitecore.Context.Database.GetItem("item path") will return expected result if the item exists in the database specified in the site configuration.

EX: For a "website" site under <sites> configuration if the database specified is "web". 

So, the item will be searched in context database "web" and returns results if the item is available.


Scenario 2: In Content Editor

    In few Scenarios, like Custom commands or buttons we may want to execute our custom code by accessing few items etc.., Sitecore.Context.Database.GetItem("item path")  will not return any result in this case.

In this Scenario, observe the url. URL contains /sitecore/shell that means currently you are in "Shell" site. Access "showconfig.aspx" admin page (ur domain/sitecore/admin/showconfig.aspx)


-> Search for <sites> tag
-> Find out shell site configurations under <Sites>
-> For <site name ="shell"> database is configured as "Core" database.
-> Got the answer? why the result is null (simply it is checking the item in Core db and the item does not exist in core DB)

What to do now? How to fetch item details in this case?

Option1 : use Sitecore.Context.ContentDatabase.GetItem("item path") -> points to master db and fetches the item if available.

Option2 : use ContextSwitcher to switch the context from core to CM website, so the db will point to "master" db.

For Switching Context:


SiteContext sitecontext = SiteContext.GetSite("sitename");
using(var context = new SiteContextSwitcher(sitecontext))
{
	//access item here
}

In Short:


// use approach1 when the code to be executed from content editor and website
// Note: To access correct database in content editor, use contextswitcher
Approach 1) Sitecore.Data.Database database = Sitecore.Context.Database;

// use approach2 approach3 when you are sure that code will be executed only in particular db 
// and not called in other scenarios
Approach 2) Sitecore.Data.Database database = Sitecore.Configuration.Factory.GetDatabase("master");
Approach 3) Sitecore.Data.Database database = Sitecore.Data.Database.GetDatabase("master");

// use approach4 to access authoring database
Approach 4) Sitecore.Data.Database database = Sitecore.Context.ContentDatabase;


Hope this post helps you. Happy Sitecoring... 😊

Comments

Popular posts from this blog

Overwriting sxa content field in solr document

Displaying Custom 404 error page for urls with unallowed file extension in sitecore

How to specify custom richtext menu as default richtext field setting when no source is specified for richtext field in sitecore

How to rebuild custom solr index programmatically in Sitecore

Handling URL requests with different file extensions in Sitecore

Rendering Parameters and Rendering Parameter Templates in Sitecore