Kamran
Kamran

Reputation: 4100

Sitecore Context use which database, Master, web or core?

Sorry if my question is too basic, I can't figure out that in the code when I write:

var myItem = Sitecore.Context.Database.GetItem("....");

Which database is used in this case, either Master Db or Web Db ? In this link, "Context Class" its written that, "The Context object holds information about the current state such as current database, current language, current domain etc."

How do I know what is current? e.g. Current Database

Upvotes: 0

Views: 6797

Answers (4)

Diego
Diego

Reputation: 1

Most of it was already answered but the answer is: it depends on what part of Sitecore is being executed. For example if you are previewing a page it by default will use the master database. But if you are viewing the published version of the site than it will use the web database.

Upvotes: 0

Gatogordo
Gatogordo

Reputation: 2635

The context database is determined by your request (there is a databaseresolver in the httprequestbegin pipeline), but can be altered in code. This means that you question is actually not answerable.. it can be anything.

Let's assume your code is running in a (sub)layout or rendering, which seems like a correct assumption and skips the more tricky parts as core database and so on. In this case your context will be determined by the sites definition. In a standard setup this will be the "web" database. But you can define other databases (to create multiple publishing targets) and then the name could be different ("pub" ...).

In editing mode however, the context database can be different things. Normally "master" when editing, "web" when previewing...

So, as the name states - it all depends on the context in which your code is running.

Upvotes: 0

Amir Setoudeh
Amir Setoudeh

Reputation: 500

The Context Database is one your site or code is actively querying against. For your website, the default database is defined in the <site> definition and that becomes the "context" database for that site. For example, by default, the "web" database is the context database for the "Website" site definition.

<site name="website" database="web" ...../>

Sitecore.Context.Database.Name will give you the name of the current Context database.

Upvotes: 1

Vlad Iobagiu
Vlad Iobagiu

Reputation: 4118

Sitecore data is stored in multiple databases. From a web developer’s perspective, the two most often used ones are master, which contains the data being edited in the Content Manager, and web which is storing the data used to display the web site.

Databases can be accessed through the Database class. To obtain a reference to a database, use the Factory class, as in:

 Sitecore.Data.Database master =  
    Sitecore.Configuration.Factory.GetDatabase("master");

The database names and implementation details are specified in the web.config below the section.

Whenever the user code is invoked by Sitecore, a so called context database is automatically assigned. You can access this database by using the Context class, as in:

Sitecore.Data.Database current = Sitecore.Context.Database;

When the code executes on the web site (that is in a layout or an xsl extension), the context database will be web. When code is executing within the Content Manager, the context database will be core. The core database contains data needed by the Content Manager.

To access the database being edited within the Content Manager, you can use

Database content = Sitecore.Context.ContentDatabase;

The ContentDatabase property will be empty when executing in the web site context. Only content editors (such as the Content Manager) will normally support this property.

Upvotes: 4

Related Questions