Monday, August 18, 2014

Sitecore Databases

Sitecore data is stored in multiple databases. We have to use Sitecore.Data.Database class to access sitecore databases. Default sitecore configuration includes three databases.

·         Master – Contains the data being edited in the Content Manager.
·         Web – Contains published versions of Master database used to display the web site.
·         Core – Contains data controlling the Content Manager.

When the code executes on the web site (that is a layout or an XSL extension), the context database will be web. When the code is executing within the Content Manager, the context database will be core.

1) Access data base by name
To obtain a reference to a database, use the Factory class.
Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase (“master”);

2) Access context database
Whenever the user code is invoked by Sitecore, a context database is automatically assigned which can be assessed using the Context class.
Sitecore.Data.Database current = Sitecore.Context.Database;

3) Access content database
Content Editor interact with the content database. The default content database is the Master database. User interfaces such as the Sitecore Desktop allow the user to change the content database to another database.

To access currently active content database we use ContentDatabase property.

Sitecore.Data.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.

4) Publish a database
Publishing copies an item from master database to the web database.

The 3 ways to publish items are

1.       Republish: This method will publish every item, even items that have not changed. If you are publishing on a large website this method of publishing can take a long time. It is intended to be used when you are publishing to a new web database, if you are restoring a backup of the master database or similar situations.

2.       Smart publish: This method compares each item in the master database with the item in the web database. In Sitecore each item has an internal revision identifier and it is this identifier that is compared. If they are different, that is, the item has been changed since it was last published, the item will be published. Even though this method is still comparing each item, it is much faster than doing a republish.

3.       Incremental publish: Every time an item is changes, it is added to the publishing queue. This applies both to changes made through the Sitecore user interface and changes made programmatically. Doing an incremental publish will only publish the items in the publishing queue.Therefore only items that has been changed will be published and sitecore does not have to do any comparisons to figure out which items has been changed. This way of publishing is therefore by far the fastest. Republish and smart publish do not use the publishing queue.

To publish the Master database incrementally in every language to the Web database
Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase (“master”);
Sitecore.Data.Database target = Sitecore.Configuration.Factory.GetDatabase (“web”);
Sitecore.Data.Database[] targetDatabases ={target};
Sitecore.Globalization.Language[] languages = master. Languages;
Sitecore.Publishing.PublishingManager.PublishIncremental (master, targetDatabases, languages);

PublishSmart () is used for smart publishing.
Republish () is used for all complete publishing of the website.

Note: If you set updateStatistics property to false while creating an item programmatically and want to use PublishSmart (), you should manually set the Revision field to a new Guid. 

No comments:

Post a Comment