2

I am currently reviewing an existing Sitecore project. One of the items has a controller rendering that outputs a form onto the Layout. In the Action Method, for the controller rendering, there is a line that seems to get the Item's Database Name credentials. I have had a look at the Item's Layout, however I can't find any Database field. I know that Sitecore.Context.Item is meant to get the current Item. However, I am know sure in the code below, how where Sitecore.Context.Item.Database.Name is pointing to. Any explanation would be really appreciated.

 public ActionResult Form()
    {
        Item currentItem = Sitecore.Context.Item;

        if (!IsValid(currentItem))
        {
            return Redirect(Sitecore.Context.Site.VirtualFolder);
        }

        FormModel model = new FormModel(currentItem);
        model.PageModel.Db = Sitecore.Context.Item.Database.Name;
        model.PageModel.ItemId = Sitecore.Context.Item.ID.ToString();

        return View(model);
    }
hoChay
  • 37
  • 10

2 Answers2

2

Sitecore.Context.Item.Database.Name provides the context database in which you are viewing the item. So if you are inside Experience Editor you will get master or if you are on site itself then you will get web.

Sitecore.Context is to provide context information like item, database or language. So for example Sitecore.Context.Item.Language will provide context language in which you are viewing the content on site.

Rohit Chopra
  • 477
  • 1
  • 4
  • 11
1

Your "Database" property is not something you will find in a field or anything - it refers to the Sitecore database where the item is located. In a simple setup that will most likely be "master" or "web". The name property of the database will just refer to a string that indicates the database (master - web - ...).

As in Sitecore your item can come from different databases, this property can be used to identify that source. Published items will in a standard setup be in the web database, the master database will contain all items and versions and is used while editing.

Gatogordo
  • 2,629
  • 2
  • 22
  • 32