Return to doc.sitecore.com

Valid for Sitecore 5.2, 5.3
How to define the database containing Items to be edited explicitly?

Q:

How to define the database containing items to be edited explicitly? 

A:

When you run some code, the default context is set to the website.
This means that all database definitions are taken from the website web.config definition: ...

<site
        name="website"
        virtualFolder="/"
        physicalFolder="/"
        rootPath="/sitecore/content"
        startItem="/home"
        language="en"
        database="web"
        domain="extranet"
        allowDebug="true"
        cacheHtml="true"
        htmlCacheSize="10MB"
        enablePreview="true"
        enableDebugger="true" />

...

As can be seen, the content attribute is missing here. That’s why the content database is undefined. In other words, Sitecore.Context.ContentDatabase returns null.  

The following actions can be taken in order to solve the problem:

  1. Change the context to the site where the content database is defined.
    Use the following statement before calling the Sitecore.Context.ContentDatabase property:

    Sitecore.Context.SetActiveSite("shell");
    or
    Sitecore.Context.SetActiveSite("modules_shell");
     
  2. Add the content attribute as shown below:
    ...
    name="website"
    virtualFolder="/"
    physicalFolder="/"
    rootPath="/sitecore/content"
    startItem="/home"
    language="en"
    database="web"
    domain="extranet"
    allowDebug="true"
    content="master"
    cacheHtml="true"
    ...