Monday, 12 March 2012

How to access Sitecore Database in Code Behind (Sitecore v6.5) - 2

There are three methods to work with Sitecore Database. To obtain it, you have to use Factory class Sitecore.Configuration.Factory 

GetDatabase("Database Name"): Get Sitecore Database by Name 
Sitecore.Data.Database database = Sitecore.Configuration.Factory.GetDatabase("DBName");
 You need to pass Database name as a parameter and it will return Sitecore Database Object.
 Note: “DBName” should match with ID of any /Configuration/Sitecore/Databases/Database and it is case sensitive. 

GetDatabaseNames(): Get all the Sitecore Database names 
string[] databases = Sitecore.Configuration.Factory.GetDatabaseNames(); 
GetDatabaseNames method returns a string array with list of Database Names added in /Configuration/Sitecore/Databases/Database. 

GetDatabases(): Get all the Sitecore Databases Object in a List
List<Sitecore.Data.Database> databases = Sitecore.Configuration.Factory.GetDatabases();
GetDatabases will return a list with all the Sitecore Database Objects configured in /Configuration/Sitecore/Databases/Database.
Note: Add Sitecore.Kernel.dll reference to project.

No comments:

Post a Comment

Articles

Automatically create new version for Lock & Edit without workflow

Sitecore will create a new version on Lock and Edit if the item is tied to a workflow. For our case, version has to be created even withou...