hoChay hoChay - 1 month ago 11
C# Question

How do you set the Database details on Sitecore.Context.Item.Database item in Sitecore

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);
}

Answer

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.