Using Sitecore Database Properties

VP, Sitecore Practice
  • Twitter
  • LinkedIn

Using Sitecore Database Properties

Recently, I was a given a task to schedule a sitecore product sync task and maintain history of run, how many units processed and persist some value until next time the task runs. The first thing that came to my mind was creating a custom database and tables to persist fields like like Last Run Time, End time. But the idea of creating a new database and creating a table that sitecore agent can update was not so productive.

I was going through the sitecore databases and found a table Properties in all the three databases master, core and web. The content of the tables looked very sitecore friendly and it immediately struck to me that by putting the data in these tables, the data could be persisted. All I had to do was find a way to perform insert and read operations on these tables. Looking at the table structure, I knew that it was all about passing a key value pair to a method.

Here the property name is sc_dbVesion and value is 500

To be honest, I initially thought there would be a class that I will have to extend and then I could perform Read and Write operations. But all I had to do was use the existing SetValue and GetValue functions.

Here are the two functions I created to store date properties. The first function is used to write a property name and value to the table and the other to read the property from the table. I am inserting the data in core database. There are other functions available for inserting long and string data type values. The example below uses date data type.

Write method

private static void SetDatePropertyValue(string propretyName, DateTime value)

{

if (Sitecore.Context.Database == null)

{

Sitecore.Data.Database core = Sitecore.Configuration.Factory.GetDatabase("core");

Sitecore.Context.Database = core;

}

Sitecore.Context.Database.Properties.SetDateValue(propretyName, value);

}

Read method

private static DateTime GetDatePropertyValue(string propertyName)

{

if (Sitecore.Context.Database == null)

{

Sitecore.Data.Database core = Sitecore.Configuration.Factory.GetDatabase("core");

Sitecore.Context.Database = core;

}

return Sitecore.Context.Database.Properties.GetDateValue(propertyName, DateTime.MinValue.ToUniversalTime());

}

To actually view the property you just created, go to the core database, run the query select * from dbo.Properties , you will see it there!

Related Blogs

Latest Blogs